JavaScript - A* Algorithm Only Finds Vertical Path?
Similar TutorialsSO, thanks for checking this out. If i have 3 text areas for output and an array that can be of size 0 to however big a user passes in values. How would I display the last 3 values of the array in the text areas? Here's what I was thinking psuedoCode, but I just want to know if there is an easier way. thnx! Code: //handling all the cases (ie if i have less than 3 items) var size = array.length display 0 if(size <= 3) if(size == '3'){ display0 = array[0] display1 = array[1] display2 = array[2] if(size == '2') do the same but copy the 2 elements if(size == '1') do the same else var newSize = (size -1) - 1 //to find where the first of the 3 elements will be display0 = array[newsize] display1 = array[newsize+1] display2 = array]newsize+2] Its seems a bit extraneous doing it this method, so i was just wondering if anyone could think of a better way. Thanks again! not sure why this wont sort in asending order... i tend to make little dumb mistakes sorry. :/ Code: <script type="text/javascript"> function sort(nums) { var rangeStart = 0; var rangeEnd = nums.length - 1; var i = 0; var minPosition = rangeStart; while(rangeStart < rangeEnd) { // find minumum for(i = rangeStart; i < rangeEnd; i++) { if(nums[i] <= nums[minPosition+1]) { minPosition = i; } } // swap var temp = nums[rangeStart]; nums[rangeStart] = nums[minPosition]; nums[minPosition] = temp; // change range rangeStart++; } } document.write("<h3>Examples</h3>"); first10 = [2,3,5,7,9,4,8,0,6,1]; document.write("<div>Sorting <tt>["+first10+"]</tt> with current code gives "); sort(first10); document.write("<tt>["+first10+"]</tt></div>"); ages = [19,34,20,66,82,53,88,74,39,13]; document.write("<div>Sorting <tt>["+ages+"]</tt> with current code gives "); sort(ages); document.write("<tt>["+ages+"]</tt></div>"); </script> Hello, I'm trying to write some javascript that will detect if the page has been loaded because of the refresh button being pressed. I've searched google on how to do this, and several websites recommend something similar to the code I'm implementing below: Code: <script type="text/javascript" language="javascript"> // in head function checkRefresh() { alert("BEFO value = " + document.getElementById("visited").getAttribute("value")); if (document.getElementById("visited").getAttribute("value") == null || document.getElementById("visited").getAttribute("value") == "") { document.getElementById("visited").setAttribute("value", "refreshed") } alert("AFTER: value = " + document.getElementById("visited").getAttribute("value")); } </script> ... <body onload="Javascript:checkRefresh();"> ... <form id="hiddenform"> <input type="hidden" id="visited" value="" /> </form> </body> My variation differs from most of the examples on the internet in a few ways (which may or may not affect its functionality): 1) Most examples access the elements by directly using their names (as in: document.hiddenForm.visited.value). I'm using document.getElementById(...).getAttribute(...) just because that seems to be the safest way to ensure you are in fact getting the elements you want, and setAttribute(...) to ensure you're setting the attribute in the proper way. This entails that I need to set the ID in the form and input elements rather than the name. 2) I'm accessing the input tag directly (rather than going through the form) because I really don't see how this would make a difference. 3) I'm doing all this within asp:content tags which, from what I understand, can affect the behavior of the elements within it. I'm not sure if this works out for other programmers, but for me it doesn't seem to be working. My alert messages in the checkRefresh function tell me that the value of the input element does indeed change as expected, but it seems to get wiped out and reinitialized to the original value of "" when the page is refreshed. Am I doing something wrong? Thanks for any help. I've written a function that "condenses" a string if it is too long. Code: function shortenMsg(msg,maxLen){ if (msg.length > maxLen){ var over = msg.length - maxLen, // amount that needs to be trimmed method1 = (msg.match(/,\s/g) || []).length, // amount that method1 can trim method2 = (msg.match(/\]\s\[/g) || []).length; // etc... method3 = (msg.match(/demonstration/gi) || []).length * 6, // etc... if (method1 >= over){ msg = msg.replace(/,\s/g, ","); } else if (method2 >= over){ msg = msg.replace(/\]\s\[/g, "]["); } else if (method3 >= over){ msg = msg.replace(/nstration/gi, ""); } else { // optimal combination of 2+ methods } } return msg; } var longMsg = "...", shortMsg = shortenMsg(longMsg, 50); // example usage Each of the methods 1-3 is the amount that that specific method can trim from the string. I'd like to be able to trim as little as possible. For example, if the string needs 5 characters to be trimmed, and method1 can trim 8 characters, but method3 can trim 6, then method3 should be used. If none of the methods can individually trim the string enough, then I'd like the optimal combination of the methods that will get the job done. I can't figure out what sort of code structure I need for this (besides a ton of if/else statements). Maybe an array that contains each of the methods, arranged in increasing order....? Help would be appreciated! Sorry for posting so much recently I have an assignment to code a java class that plays rock, paper, or scissors. The class can access the history of past gestures played by both itself and its opponent, but nothing else. The class will be played against others in 10,000 matches and the winner will be determined via round robin format. Other than using a greedy algorithm to determine statistically the best choice from prior gestures and basic pattern recognition I have no decent ideas. I'd appreciate any suggestions.
Hi all First post. I don't normally use Javascript apart from a few browser workarounds, but I have a requirement to set a country cookie and it was working when all my pages were in the same directory, but now that I've added subdirectories, it sets a new cookie for each directory instead of one sitewide. Here's the code (which I edited from some found on the internet): Code: // JavaScript Document <!-- function ReadCookie() { var NameOfCookie="Country"; if(document.cookie.length > 0) { begin = document.cookie.indexOf(NameOfCookie+"="); if(begin != -1) { // our cookie was set. // The value stored in the cookie is returned from the function begin += NameOfCookie.length + 1; end = document.cookie.indexOf(";",begin); if(end == -1) end = document.cookie.length; country=(document.cookie.substring(begin,end)); if (country=="HongKong")document.location.href='hk/'; if (country=="Australia")document.location.href='au/' if (country=="NewZealand")document.location.href='nz/' } } } function SetCookie(cookieName,cookieValue) { var today = new Date(); var expire = new Date(); var nDays=365 expire.setTime(today.getTime() + 3600000*24*nDays); document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString(); } //--> Then on the homepage links I use: Code: <a href="hk/" onClick="SetCookie('Country','HongKong');">Hong Kong</a> Then on the subdirectory pages, I have a reset button which allows you to return to the homepage and disable the redirect. Code: <a href="../" onClick="SetCookie('Country','Null');">HONG KONG</a> So I just need the path=/ to be added somewhere is that right? I tried changing the last line of the main code to: Code: + ";expires="+expire.toGMTString()+" path=/"; but that only worked in IE not Firefox. Can anyone help? Thank you! thingswelike Hi, I have setting some cookies to track visitors to my site. I have my pages in folder music and in sub folders. I can access my site by typing: www.ms.webspace.com/sara/music So the domain will be: www.ms.webspace.com and the path: /sara/music/ but the broblem is that other cookies are attached to my cookieby going to www.ms.webspace.com or any link in it. Is the problem from setting the path? If not how can I filter my cookie so i can just display my cookies only, I use one function to display all the cookies. Thanks. Hi all, I have been searching for a long time to obtain the UNC path of a file using the FileUpload control using javascript.. Is there any way to obtain it. Can anyone please guide me to the answer. Thank you, Aswin Unfortunately, this isn't standard cookie stuff (I don't think). I have a compiled .exe loader for a program which includes an .html page which records the input of a textbox to a cookie. That much works OK. But reading it back isn't working. It seems the path is: ~~local~~/C:/Users/Terry/AppData/Local/Temp/td9/FrontPage%20Webs/1001(FINAL)(v1.2)/loader/ i.e. a temp dir related to the directory from where the .exe file was created. What I need is for the cookie to be available everywhe ~~local~~/ Is anyone able to shed some light? Adding ";path=/"; isn't working. I guess it's something to do with being compiled? Fairly new the javascript, as in only been tinkering around for about a day. So forgive me if this is a simple question. At the moment I have the following function for handling events such as onclick, mouseover, mouseout. Code: function switcher(action, img){ switch(action){ case "hover": //mouse is on image document.imgSeat.src = "images/hover.gif"; break; case "click": //mouse has clicked image document.imgSeat.src = "images/click.gif"; break; default: //mouse elsewhere document.imgSeat.src = "images/defaulter.gif"; break; } } And the html the function refers to Code: <body> <div id="imagesholder"> <a href="#nerf"><img border="0" id="seat0" src="images/available.gif"></a> </div> </body> What im trying to do change my function so that instead of it being hard coded for the id "imgSeat" i can use the id passed through the parameter "img" instead. However I seem to be banging my head against the wall. I've tried using the following Code: document.getElementById(img).src = "images/available.gif"; //no error, just does nothing //where img is "0" instead of "seat0" document.seat[img].src = "images/available.gif"; //error: document.seat is undefined Would be extremely greatful if someone should push me in the right direction or show me what im doing wrong. This page should work, but it doesn't. My debugging says that I have a invalid character on line 2 and on line 39 it is "object expected". Here's the page: <HTML> <HEAD> <TITLE>The Golf Page</TITLE> <STYLE> BODY {font-family:Arial, Helvetica, sans-serif; font-size: 18pt; color:blue; background-color:rgb(255,255,128)} </STYLE> <SCRIPT SRC="Ball.gif"> var x = new Array(-395, -389, -383, -377, -371, -365, -359, -353, -346, -340, -334, -328, -322, -316, -310, -304, -297, -291, -285, -279, -273, -267, -261, -255, -248, -242, -236, -230, -224, -218, -212, -206, -199, -193, -187, -181, -175, -169, -163, -157, -150, -144, -138, -132, -126, -120, -114, -108, -101, -95, -93, -91, -88, -86, -83, -81, -78, -76, -73, -71, -69, -66, -64, -61, -59, -56, -54, -51, -49, -47, -44, -42, -39, -37, -34, -32, -29, -27, -24, -22, -20, -17, -15, -12, -10, -7, -5, -2, 0); var y = new Array(-300, -300, -300, -299, -298, -297, -296, -294, -292, -290, -288, -285, -282, -279, -276, -272, -268, -264, -260, -255, -250, -245, -240, -234, -228, -222, -216, -209, -202, -195, -188, -180, -172, -164, -156, -147, -138, -129, -120, -110, -100, -90, -80, -69, -58, -47, -36, -24, -12, 0, -5, -10, -14, -18, -22, -25, -29, -32, -34, -37, -39, -41, -43, -45, -46, -47, -48, -48, -48, -48, -48, -48, -47, -46, -45, -43, -42, -40, -37, -35, -32, -29, -26, -23, -19, -15, -11, -6, 0); index=0 function moveBall() { if(index <=[x.length-1]) { placeIt("Ball", x[index], y[index]); index++; setTimeout("moveBall()", 5); } else { ("showIt()", 5); setTimeout("showIt('Slogan')", 5); setTimeout("showIt('Slogan1')", 10); setTimeout("showIT('Marquee')", 15); } } </SCRIPT> </HEAD> <BODY onLoad="moveBall(Ball);"> <DIV ID="Marquee" visibility:hidden font-family: Time New Roman, Times, serif; font-style:italic> <CENTER> <MARQUEE BGCOLOR="#BBBBBB"> A beautiful day for GOLF...Sunny...No WINDS...TEMP:68-70 </MARQUEE> </CENTER> </DIV> <SCRIPT SRC="Golf.js"></SCRIPT> <DIV ID="Title" STYLE="border-left:1px solid blue; border-right:3px solid blue; border-top:1px solid blue; border-bottom:3px solid blue; padding-left:395;padding-top:260;padding-bottom:0; background-color:rgb(0,255,0)"> THE G<SPAN ID="Ball" position:relative; left:0; top:0><IMG SRC="Ball.gif" BORDER=0 width="17" height="17"></SPAN>LF PAGE </DIV> <DIV ID="Slogan" STYLE="color:black; font-family: Times New Roman, Times, serif; font-style:italic; font-weight:bold; position:absolute; left:120; top:100; z-index:2; visibility:hidden"> Your Online Source of Golf Equipment </DIV> <DIV ID="Slogan2" STYLE="color:white; font-family: Times New Roman, Times, serif; font-style:italic; font-weight:bold; position:absolute; left:121; top:101; z-index:1; visibility:hidden"> Your Online Source of Golf Equipment </DIV> </BODY> </HTML> The function moveBall and its contents should be checked as well, although it should be correct. But the page won't load properly. The marquee is suposed to be hidden until the function executes, and it is displayed. As far as I know the only errors are in the onLoad and function, everything else is right. Any help would be appreciated. Thanks Hi, I have a script that sets a number of cookies based on a set of results returned from an external website. The file sits in the cgi-bin folder (it has to, as this is where the data is processed and results returned) and when the data is returned it is stored in a number of cookies before redirecting to a results page in the website root to display the data. The problem, before I display any code, is that one of the cookies is setting its path to the /cgi-bin folder, meaning when the page redirects back to the website this particular cookie is not accessible. However, what is even more unusual is that this only happens in Safari. Here is the code: Code: <script> // function to set cookie function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = c_name + "=" + value + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + "; path=/"; } </script> <!-- the divs below are populated with data --> <div style="display:none;" id="listcount"><!--matchcode name="LISTCOUNT" --></div> <div style="display:none;" id="nearrecs"><!--matchcode name="NEARESTRECORDS" --></div> <div style="display:none;" id="distances"><!--matchcode name="MILES" --></div> <div style="display:none;" id="ambigrecs"><!--matchcode name="AMBIGLIST" --></div> <script type="text/javascript"> // data is extracted from the divs above var ncSnip = document.getElementById("nearrecs").innerHTML; var ncDistances = document.getElementById("distances").innerHTML; var listCount = document.getElementById("listcount").innerHTML; var ambig = document.getElementById("ambigrecs").innerHTML; if (ambig != "") { //document.getElementById("result").innerHTML = "ambig"; setCookie("ckAmbig", ambig, 365); setCookie("ckResults", ncSnip, -1); setCookie("ckDistances", ncDistances, -1); setCookie("ckListCount", listCount, 365); window.location="http://xxxxx"; } else if (ncSnip != "") { //document.getElementById("result").innerHTML = "records"; setCookie("ckDistances", ncDistances, 365); setCookie("ckResults", ncSnip, 365); setCookie("ckAmbig", ambig, -1); setCookie("ckListCount", listCount, 365); window.location="http://xxxxxx"; } else { //document.getElementById("result").innerHTML = "none"; setCookie("ckAmbig", ambig, -1); setCookie("ckResults", ncSnip, -1); setCookie("ckDistances", ncDistances, -1); setCookie("ckListCount", listCount, 365); window.location="http://xxxxxx"; } </script> The specific cookie that is being set to the wrong path is the 'ckResults' cookie. Can anyone help? Does the code above shed any light as to why this one cookie would set an incorrect path in Safari only? Is this possible? Let me clarify: I need a way for a html page or something similar be able to determine what it's full path is. It must self-determine this. Can javascript help me with this? Any recommendations of what route I should take to figure this out? Thanks! I'm working on a site built through Squarespace, and I've run into some limitations. They've told me it's possible to build a Javascript to pull specific images from one page's journal entries and place them (with links to the entry) on other pages. Can somebody send me in the right direction on how I would do this? Basically I want to find a most recent posting tagged with a specific category, find the file file path of it's related image, and make that appear on the page that my javascript is on. Hi all, I have an .xsl file that reads an xml file and populates an html file and that is working well. I want to have many xml files (one per project) and one .xsl and html file. The goal would be to get the user to browse for the particular xml file they want to use and have the html file reference the path and file name. There will be a main page where the user will browse for the xml file in question. When they hit submit, the information (document path) is passed to the html file and the html file opens with information from the xml page they selected .In my html file I have the following: Code: xml.load(''12345.xml'') Basically I would like to somehow browse to the xml file in question and pass that variable (file path and name) to the html file and replace "1234.xml" with the path and name selected by the browse section. Does this make sense? Thanks. Reply With Quote 01-22-2015, 11:04 PM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,310 Thanks 82 Thanked 4,754 Times in 4,716 Posts The easiest way to do this would be via server-side code. For example, just have a PHP page the includes the desired ".xml" file into a page that is already setup with the XSLT. I vaguely recall reading that there is a way to "apply" XSLT to a dynamic set of XML, but the function named apply() in XSLT doesn't do anything like this. Still, the easiest way to control this would be via the server side code. Heck, that would even allow you to string together "pieces" of XML from various files to create the final document. is there some way to searching through multiple path or recursively searching through a path (folders and subfolders) using the code below which seems to search only one node in the tree? Many Thanks. there are basically subfolders in \documents and then subfolders therein. i would like to search it therein for all kinds of files/filenames based on "search criteria". one more thing - is it ok/advisable to use active x objects considering most modern browsers have it disabled or give warnings? Code: <script> var Fo =new ActiveXObject("Scripting.FileSystemObject"); var StrOut = new String(); var FileName = new String(); var Extention = new String(); function FindFile(FOo) { var FSo = new Enumerator(FOo.Files); for(i=0;!FSo.atEnd();FSo.moveNext()) { if(FileName == "*" || FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1) if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){ StrOut += "<tr "+ ((i%2)? "":"bgcolor=#C4E3F2") +"><td width=50%><font class=find>" + FSo.item().name + "</font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</font></td></tr>"; i++ } } } function Search() { FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name if(path.value.length>0 && Fo.FolderExists(path.value)){ StrOut = "<table border=0 width=100% cellspacing=0>" FindFile(Fo.GetFolder(path.value)); outPut.innerHTML = StrOut+"</table>"; } else alert("Insert Correct Path Address"); } </script> Code: <BODY topmargin="0" leftmargin="0"> <table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr> <td dir="ltr" bgcolor="#FFD9D9"><b><font face="Verdana" size="2">Filename : </font></b> </td> <td dir="ltr" bgcolor="#C4E3F2"> <input size=50 type=text id=search name=search class="Find"></td> </tr><tr> <td dir="ltr" bgcolor="#C4E3F2"> <p dir="ltr"></td> <td bgcolor="#FFD9D9"><input size=50 type=hidden value="\Documents" id=path name=path class="Find" > <input type=button value="Search" onClick=Search() class="Find"></td> </tr><tr> <td colspan=2 align=left bgcolor="#FFFFFF"><font face=Verdana size=2><b>Search Result</b></font><hr></td> </tr><tr> <td colspan=2 bgcolor="#FFFFFF"><div id=outPut></div></td> </tr></table> </BODY> </HTML> I've done LOTS of Googling on this subject. I can write cookies beautifully to '[other data]; path=/Stats' but I can't figure out how on EARTH to read from it. How do I access the data there? If I just say path=/ then it will read beautifully, but I need to be able to have different paths for what I want this to ultimately do.
|