JavaScript - How Can I Make My Redirect Faster?
Hello. When a user first visits /index.html, they select their city. On later visits, I redirect them to their city (ie. /vancouver.html). The script seems to run slowly, and loads the whole /index.html for a second before redirecting. How can I make it faster? I have tried placing my code 1) in the head just below the title, 2) in the head before anything else, 3) in <body onload="...">
Code: function redirect() { userCity = readCookie ("user_city"); if (userCity) { window.location.replace(userCity + ".html"); } } Could I use something like if (navigator.cookieEnabled) to run the redirect() function if cookie detected and somehow cancel loading the rest of index.html? (if no cookie, or not enabled, still load index). Or is their a more standard way? I am a novice and really appreciate any help. Similar TutorialsHow do I make my Script load faster? Is there Code that will make my Script load faster, (so that it won't take so long to view) If so, can someone show me how to incorporate it into my Example Script? With appreciation Hi, i have this code that works, but takes like 3 seconds to load when using it on the website. Is there a better way to code this to make it faster? Code: function onTaskChange(num) { if( num = 1) { var stask = document.forms[0].task11.value; setSelectValue("forms[0].task21", stask); setSelectValue("forms[0].task31", stask); setSelectValue("forms[0].task41", stask); setSelectValue("forms[0].task51", stask); for( i = 0; i < 7; i++) tblProjectHours[i][6]=stask; onTaskChange(4); onTaskChange(7); onTaskChange(10); onTaskChange(13); } else if( num = 2) { var stask = document.forms[0].task12.value; setSelectValue("forms[0].task22", stask); setSelectValue("forms[0].task32", stask); setSelectValue("forms[0].task42", stask); setSelectValue("forms[0].task52", stask); for( i = 7; i < 14; i++) tblProjectHours[i][6]=stask; onTaskChange(5); onTaskChange(8); onTaskChange(11); onTaskChange(14); } else if( num = 3) { var stask = document.forms[0].task13.value; setSelectValue("forms[0].task23", stask); setSelectValue("forms[0].task33", stask); setSelectValue("forms[0].task43", stask); setSelectValue("forms[0].task53", stask); for( i = 14; i < 21; i++) tblProjectHours[i][6]=stask; onTaskChange(6); onTaskChange(9); onTaskChange(12); onTaskChange(15); } else if( num = 4) { var stask = document.forms[0].task21.value; for( i = 21; i < 28; i++) tblProjectHours[i][6]=stask; } else if( num = 5) { var stask = document.forms[0].task22.value; for( i = 28; i < 35; i++) tblProjectHours[i][6]=stask; } else if( num = 6) { var stask = document.forms[0].task23.value; for( i = 35; i < 42; i++) tblProjectHours[i][6]=stask; } else if( num = 7) { var stask = document.forms[0].task31.value; for( i = 42; i < 49; i++) tblProjectHours[i][6]=stask; } else if( num = 8) { var stask = document.forms[0].task32.value; for( i = 49; i < 56; i++) tblProjectHours[i][6]=stask; } else if( num = 9) { var stask = document.forms[0].task33.value; for( i = 56; i < 63; i++) tblProjectHours[i][6]=stask; } else if( num = 10) { var stask = document.forms[0].task41.value; for( i = 63; i < 70; i++) tblProjectHours[i][6]=stask; } else if( num = 11) { var stask = document.forms[0].task42.value; for( i = 70; i < 77; i++) tblProjectHours[i][6]=stask; } else if( num = 12) { var stask = document.forms[0].task43.value; for( i = 77; i < 84; i++) tblProjectHours[i][6]=stask; } else if( num = 13) { var stask = document.forms[0].task51.value; for( i = 84; i < 91; i++) tblProjectHours[i][6]=stask; } else if( num = 14) { var stask = document.forms[0].task52.value; for( i = 91; i < 98; i++) tblProjectHours[i][6]=stask; } else if( num = 15) { var stask = document.forms[0].task53.value; for( i = 98; i < 105; i++) tblProjectHours[i][6]=stask; } } I have a single webpage that contains information on all 50 U.S. states. There are 50 links at the top to jump down to the state you want, and at the bottom of the information for each state a Back to Top link. I'm making the Back to Top link into something more complex, and it will require three or four lines of code. So that I don't have to repeat the code 50 times, and create a burden when I need to edit it, I want to place it in a .js file and call it x. Then below the information for each state I'll simply have: Code: <script language="JavaScript">document.write(x)</script> Does calling code from a .js file 50 times slow down the page load? Which method would load faster? Or is the difference negligible? Thank you, Peter I have this code Code: <script language=javascript> <!-- redirect to profile.php var user = location.search; window.location="profile.php" +user; // --> </script> but I want it to do this profile.php?epuid=<?=bin2hex($session->username);?> how would I do this as the java code up the top takes the user nickname from chatroom Okay, so I built up 2 codes, which do the same cause. but which one would be most efficient? or maybe which one would be best compatible for browsers? CODE 1 : - this code allows for me to use an #id Code: <a onclick="trackClick('1');" href="http://www.google.com" rel="nofollow" target="_new">(track 1)</a> function trackClick(trackid) { ajax = ajaxFunction(); var url = base_url+ "app_trackClicks.php"; url = url+ "?trackid=" +trackid; ajax.open("GET",url,true); ajax.send(null); } CODE 2 - using the REL Code: <a rel="randomclicks" href="http://www.google.com" rel="nofollow" target="_new">(track 2)</a> function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function'){ window.onload = func; } else { window.onload = function(){ oldonload(); func(); } } } addLoadEvent(randomclicks); function randomclicks() { if (!document.getElementsByTagName){ return; } var anchors = document.getElementsByTagName("a"); // loop through all anchor tags for (var i=0; i<anchors.length; i++){ var anchor = anchors[i]; if ((anchor.getAttribute("rel") == "randomclicks")){ anchor.onclick = function () { ajax = ajaxFunction(); var url = base_url+ "app_trackClicks.php"; url = url+ "?randomclicks"; ajax.open("GET",url,true); ajax.send(null); }; } } } Thanks Hi, I'm trying to change a piece of code I found in a scipt: Code: if (errors == null) { jQuery('#requestform') .hide() .html('<div class="thankyou"><h1>Merci</h1><p>Nous reviendrons vers vous.</p></div>') .show(); } Instead of the .html () I would like to redirect to another webpage of my website, in the same window. Anyone can help me? Thanks. Hey all im new to the forum as i have just started playing round with javascript but anyway i have this code PHP Code: setTimeout(top.location='profilec.php?id=' + id,4000); and when executed the time delay is not taken into account at all and completly ignored. If i remove the id var from the code and just have PHP Code: setTimeout(top.location='profilec.php?id=',4000); its fine can anyone tell me why this is happening and if there is a solution :-) thanks before hand mike i'm a total noob with java and can't seem to figure this out. i want to add a bit on to the current url : this is the code i'm using , it just keeps adding... Code: window.location=(window.location+"?p=62"); am i even doing this right.. any help appreciated.. what wrong, it just adds the website to the end of the address bar, and it doesnt redirect???help please <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div> <h1>hello</h1> <form onsubmit="return go();"> <select name="website" id="website"> <option value="http://www.google.com/">google.com</option> <option value="www.yahoo.com">yahoo.com</option> </select> <script type="text/javascript"> function go() { var s = getElementById("website"); window.location = s.options[s.selectedIndex].value; return false; //prevents the form from being submitted to the server. } </script> <input type="submit"> </form> </div> </body> </html> Hi, I'm trying to redirect my users to their profile, but can't figure out how to put the function into the link. sethighlightedValue(); is a function that returns the users username. Is there anyway else to do this? Here's what I got: Code: case RETURN: window.location = "profile.php?user='setHighlightedValue();'"; this.setHighlightedValue(); bubble = false; break; Hi guys, I have this script to print a page, which it does fine. However, after the script runs, the browser redirects to the folder root: Code: <SCRIPT Language="Javascript"> //PRINT PAGE FUNCTION function printit(){ if (window.print) { window.print() ; } else { var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; document.body.insertAdjacentHTML('beforeEnd', WebBrowser); WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = ""; } } </script> How can I stop this happening? I want remain on the current page! Hello All, I have some javscript running on a page to automatically redirect the user to the SSL secured page (https). The code I have works fine when there is www. in front of the site name, but if you just type the site name without the url the code doesn't work. Any suggestions? Thank so much. Code: <script language="javascript" type="text/javascript" > // This is the hostname that will trigger enforcement of redirection. var enableRedirect = true; // This is the hostname that will trigger enforcement of redirection. var externalHostname = "www.exelonenergy.com"; // This is the reconstructed URL if the current URL contains the externalHostname: var redirectURL = "https://" + externalHostname + window.location.pathname; // If the URL contains the externalHostname specified above, // and the protocol in plain http:, then redirect to the https: equivalent: if (enableRedirect == true) { if (window.location.hostname.indexOf(externalHostname) >= 0) { if (window.location.protocol == "http:") { window.location.href = redirectURL; } } } </script> Hi I have several checkboxes in a switch statement like the one below: Code: function whatsChecked(obj) { var indx = obj.id.substring(obj.id.length-1, obj.id.length); switch ( indx ) { case '9': if (document.sport.soccer_9.checked) { //window.open.href = "../google.com";; window.open("../google.com"); } break; } } how can I use a continue button to determine which checkbox was clicked and redirect me to the correct page? Hello everyone... I have Javascript code here which gives the time (hours, minutes, seconds) and date (year, month, day), and it updates every second. What I want to do is redirect to a certain page every 10 minutes, on the 10th, 20th, 30th, 40th, 50th and 60th minutes. The time displays and updates every second, but no redirect happens. Please fix my code! Thanks in advance. Code: <tr><td align="center"> <SPAN ID="clock"><?php echo date('d/m/Y <\b\r> H:i:s', time()); ?></SPAN> </td></tr> <SCRIPT type="text/javascript"> var year2 = <?php echo date('Y', time()); ?>; var month2 = <?php echo date('m', time()); ?>; var day2 = <?php echo date('d', time()); ?>; var hour2 = <?php echo date('H', time()); ?>; var minute2 = <?php echo date('i', time()); ?>; var second2 = <?php echo date('s', time()); ?>; var daysInMonth = <?php echo date('t', time()); ?>; function updateClock() { second2+=1; if(second2>=60){second2=0;minute2+=1;} if(minute2>=60){minute2=0;hour2+=1;} if(hour2>=24){hour2=0;day2+=1;} if(day2>daysInMonth){day2=0;month2+=1;} if(month2>12){month2=0;year2+=1;} byt(nollor(day2)+"/"+nollor(month2)+"/"+year2+"<br />"+nollor(hour2)+":"+nollor(minute2)+":"+nollor(second2),"clock"); updateUtlogg(); redirect(minute2,second2); } var utloggUtloggad = false; function updateUtlogg() { if(utloggUtloggad) { return true; } return(true); } function nollor(nummer) { if(nummer<10) { tmp="0"+nummer; } else { tmp=nummer; } return tmp; } function byt(txt,lager) { if(document.getElementById(lager)) { document.getElementById(lager).innerHTML=txt; } } function redirect(x,y) { if(nollor(x)==0 && nollor(y)==0 || nollor(x)==10 && nollor(y)==0 || nollor(x)==20 && nollor(y)==0 || nollor(x)==30 && nollor(y)==0 || nollor(x)==40 && nollor(y)==0 || nollor(x)==50 && nollor(y)==0) { setTimeout("location.href = updateInterval.php;",0); } } var oInterval = window.setInterval("updateClock()",1000); </SCRIPT> I need a weird browser redirect. I need a text box, we will call it variable "A", and a submit button on a form. when the person types in a number such as 51225 into the text box and clicks submit it will redirect them to the page yourdomain.com/51225.php. What is the best way in doing this? Code: <html> <head> <script type="text/JavaScript"> <!-- A=document.form.ID.value function onclick(event) { window.location = http://yourdomain.com/document.write(A).php" } //--> </script> </head> <body> <form name="form"> ID: <input type="text" name="ID" size="6" value=""> <input type="submit"> </form> </body> </html> I know this is not correct but it is a start. I can't figure out how to place the variable into the url. Hi, I want to do a redirect count so that the user can see the count going down. for example, I want them to be able to see the following. You will be redirected in 5 seconds. The user should be able to see the count going down from 5 to 4 to 3 to 2 to 1 and then they are redirected. I can get the page to redirect after 5 seconds but I want the user to be able to see it count down. Thanks in advance for your help I want to be able to put up files and assign passwords to individuals so that they can access one of those files but not all. In other words person A enters password & is redirected to file A; person B enters password & is redirected to file B, etc. Anyone have an example or know where I can get an example of a script like this? I am not a programmer so the simplier the better. Thanks
Hi, I have a form in which i have a dropdown list and a submit button. I want to code so that when users select the value from the dropdown list, it should redirect then to the specified page. For instance i have the value, "car", "House" and "pets" in my dropdown listbox, if a use click on "car" it should redirect them to the car page. I have done the following code, but something seems not to work Code: <script type="text/javascript"> function move(){ if (document.sell.cat.value == 'Cars'){ window.location = 'http://www.yourdomain.com' } </script> and in the submit button am calling the function move() Code: <input type="submit" name="button2" id="button2" value="Search" onclick="move()"/> my form name is sell and the dropdown list menu is cat. Thanks I have data pages in my e-commerce site (from 1999) that are imbedded in a parent page using the object tag. I have started to include a little JS snippet that is intended to redirect to the parent if the embedded pages are loaded separately: Code: <script language="JavaScript" type="text/javascript"> try { if (top == self) {top.location.href='parentpage.htm'; } } catch(er) { } </script> This works fine in Firefox, but in IE 7 and 8 it gets into an infinite loop of reloading the parent page into the object frame. Here is a sample: http://www.greathallminis.com/Ancien...s_anc15_mm.htm (I've left this page with the issue, while I have excluded the script for IE elsewhere.) Is there an alternative and equally simple script for IE, or can I modify this to work for IE? Thanks, Rob |