JavaScript - Redirect Driving Me Crazy Javascript
This has been driving me crazy all day, cant seem to work out what im doing wrong.
Alerts the correct URL Used document.write and it works But the redirect command doesnt initiate automatically on click and im not sure why. Someone help!! Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>http</title> <style> <!-- p.MsoNormal {mso-style-parent:""; margin-bottom:.0001pt; font-size:11.0pt; font-family:"Calibri","sans-serif"; margin-left:0cm; margin-right:0cm; margin-top:0cm} --> </style> <script type="text/javascript"> function buildstring(form) { var text = form.text.value; var xmljson = form.choice1.value; var init = 'localhost:1234/PPTX/tr?service=extract&action=extractAll&text='; var op = '&op='; var first = init + text + op + xmljson; var complete = "http://" + first; alert (complete); window.location = complete; } </script> </head> <body> <form method="get" id="urlbuild"> <table> <tr> <td> <h2>URL Builder</h2> </td> </tr> <tr> <td> <input type="text" size="100" name="text" value=""> </td> </tr> </table> <table> <tr> <td> <select type="one" name="choice1"> <option value="xml"> XML </option> <option value="json"> JSON </option> </select> </td> <td> <p>Please choose XML or JSON.</p> </td> </tr> </table> <table> <tr> <td> <input type="submit" onClick="buildstring(this.form)"> </td> </tr> </table> </form> </body> </html> Similar Tutorialshi... I'm trying to pass this string and I keep getting the "missing ; before statement error" which I know means I not escaping something or something, but I've tried it every way I can think of and still no dice... any ideas? Code: content='<a href onclick="javascript: document.getElementById('searchbox').style.display='inline'">here<\/a> to enter your address' +'<div id="searchbox" display="hidden"><form action="javascript:getDirections()">' +'<input type="hidden" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +'<div>'; thanks in advance... Hi, I read several examples, tried them all out - and nothing works! Driving me crazy - completely insane! Maybe you guys can help My problem is simple: file B.html includes this line: Code: <script type="text/javascript" src="B.js" defer="defer"></script> file B.js is a regular JS file, yet I need to insert a PHP function in it (yeah - I know it will operate server-side before the page reaches the browser). I did something like: Code: (some js code) var user_id = <?php $a=100; $b=200; $c=$a+$b; echo($c);?>; alert("Your user ID is:" + user_id); (some more js code) (What I expect is the browser to see the line as: var user_id = 300; ) Doesn't work. Renaming B.js to B.php did nothing. What am I doing wrong? Thanks! long story short, I have been trying to run a loop, say, 10 times and I know the result I want will be on the first request but it sends all 10 at the same time, over writing the variable for xmlhttp. discovering in my days of attempts that its just the nature of the beast, but I would like ANY way possible to get a response on the 1st pass of the loop in any way possible. But for now, here's a riddle I can't solve, how to make this function return with the response? Code: function ajaxRequest(query){ var response = 'I have not checked yet.'; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { response = xmlhttp.responseText.trim(); } } xmlhttp.open("GET",query,true); xmlhttp.send(); return response; } It will always instantly say "I have not checked yet." when I would like the response. I'm okay with it being a 2-5 second delay as long as it doesn't pause the browser like a synchronous request. Even a delay of some sort just before the return would probably do it. In that case, I think I can get the rest. Thanks much in advance! Hi, So I think I can't see the trees for the forest. I'm pretty good at code and usually work out the answer myself. I am doing some work for a client, working on some code written by someone else. It is a form, and for some reason the validation isn't working. The form sends with or without data entered into the mandatory fields. I can't look at the code anymore.. Is this the correct place I would ask for help with such a thing? Thanks kindly. I'm trying to figure out how to use an javascript function to redirect without using <body onload>. Normally I use this: Code: <META HTTP-EQUIV="Refresh" Content="0; URL=ajax.php?page=mypage"> But since I'm using ajax, and only want to redirect tha ajax part, the above can not be used... When I have a hyperlink to go to a page in ajax it looks like this: Code: <a href="#" onclick="load('ajax.php?page=mypage','contentarea');return false;"> Is there anyway that I can have the ajax part set into the META HTTP-EQUIV="Refresh" or is there an other way to do this? Thanks in advance... Hello Coding Forums, For the past few days, i have been learning and just surfing the forum board. I have recently ran into a problem and i am in need of help. This is a mixture of HTMl, XML, and JavaScript. For what i know, it seems my problem is a JS problem. I have been looking at this Facebook Fan page: http://www.facebook.com/pages/The-fi...18916928166010 I understand everything in the second page. The problem is i dont understand how in page 1, you are direct to page 2. Can anyone please explain to me how this is working? I am new with javaScript. Trust me, i have looked and i am still learning. This is important to me. -Mygel Hi I was wondering how to redirect to another page when a button is clicked using javascript. Should I use the goSite() function, or window.location.href =
Hi, I want to redirect pages e,g 'http://mydomain.com/pagename.html' to 'http://www.mydomain.com/pagename.html",i.e if anyone access without www then it will be added automatically,how can i do this using javascript? plz help Thanks hey guys. i need a js code that redirects to a flash page or to a non flash page after detecting the browsers plugin abilities and if it has flash or not. any help would be appriciated. cheers Hi, I am very new to Javascript but have found a great tutorial to create a dynamic dropdown menu. I got it to work, but have problems redirecting it to the correct page after the necessary page refresh to load the items in the 2nd dropdown menu. The Javascript in the header is the following: Code: <SCRIPT language=JavaScript> function reload(form){ var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='index.php?cat=' + val ;} </script> My PHP controller (index.php) code to load the correct frames is as follows: Code: <ul id="navbar"> <li><a href="index.php?id=home">Home</a></li> <li><a href="index.php?id=overview">Overview</a></li> <li><a href="index.php?id=add">Add new product</a></li> </ul> Code: if(isset($_GET['cat'])){ include ('add.html.php');} else { switch($_GET['id']) { default: include('home.html.php'); break; case "home": include('home.html.php'); break; case "overview": include('add.html.php'); break; case "add": include('add.html.php'); } } So I have made a workaround to load the "add.html.php" frame with the if(isset)$_GET['cat']. But what I really would like to do is to change the Javascript self.location to Code: self.location='index.php?id=add?cat=' + val ;} so that the PHP index script loads the add.html.php automatically. However, when changing the self.location as described above the page is redirected to home.html.php. It is really annoying, I have googled for a full day but haven't found a solution yet. Maybe someone can tell me how I can easily load the correct frame by using Javascript? Many thanks in advance!! hi, I am trying to redirect from my main site to my mobile site by detecting screen width using javascript. But everything time i try to test this via the iphone 4s,the browser keeps reloading without loading any content, like its on some sort of loop! Here are the scripts ive tried to used below, have i missed anything? [CODE] <script type="text/javascript" charset="utf-8"> if (navigator.userAgent.match(/(operamini|ipad|ipod|iphone|android|webOS|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafon e|o2|pocket|kindle|mobile|hiptop|\bpda|psp|treo|nokia|blackberry)/i)) { location.replace("example.com"); } </script> [CODE END] [CODE] <script type="text/javascript"> if (screen.width <= 550) window.location = 'http://yoursite.com/mobile'; </script> [CODE END] Thanks in advance for any help! Jon Hi, Ever seen on the iphone where you have to slide the slider across the screen to unlock it? I have a duplicate of this using javascript but no matter what I try I cannot make it redirect. I want the page to be redirected after the slider has gone to the other side. Heres the HTML Code: <!DOCTYPE html> <head> <meta charset='UTF-8'> <title>Slide To Unlock</title> <link rel='stylesheet' href='css/style.css'> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script> <script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script> <script src='js/slidetounlock.js'></script> </head> <body> <div id="page-wrap"> <div id="well"> <h2><strong id="slider"></strong> <span>slide to unlock</span></h2> </div> </div> </body </html> And the javascript Code: $(function() { $("#slider").draggable({ axis: 'x', containment: 'parent', drag: function(event, ui) { if (ui.position.left > 550) { $("#well").fadeOut(); } else { // Apparently Safari isn't allowing partial opacity on text with background clip? Not sure. // $("h2 span").css("opacity", 100 - (ui.position.left / 5)) } }, stop: function(event, ui) { if (ui.position.left < 551) { $(this).animate({ left: 0 }) } } }); // The following credit: http://www.evanblack.com/blog/touch-slide-to-unlock/ $('#slider')[0].addEventListener('touchmove', function(event) { event.preventDefault(); var el = event.target; var touch = event.touches[0]; curX = touch.pageX - this.offsetLeft - 73; if(curX <= 0) return; if(curX > 550){ $('#well').fadeOut(); } el.style.webkitTransform = 'translateX(' + curX + 'px)'; }, false); $('#slider')[0].addEventListener('touchend', function(event) { this.style.webkitTransition = '-webkit-transform 0.3s ease-in'; this.addEventListener( 'webkitTransitionEnd', function( event ) { this.style.webkitTransition = 'none'; }, false ); this.style.webkitTransform = 'translateX(0px)'; }, false); }); I have a simple javascript for validating the radio buttons. But for what ever reason window.location does not redirect. this is my code: <SCRIPT LANGUAGE="JavaScript"> function checkRadio (frmName, rbGroupName) { var radios = document[frmName].elements[rbGroupName]; for (var i=0; i <radios.length; i++) { if (radios[i].checked) { return true; } } return false; } function valFrm() { if ( checkRadio("frm1","radio1")==true && checkRadio("frm1","radio2")==true) window.location="price.jsp"; else alert("You didnt select any price"); } </script> when i replace an alert with the redirect line it works fine. any help would be appreciated. The follow code DOES WORK, however not as I want it TO. I have a domain FRAMED / Redirect to a free blogging site. at the BLOGGING site I have to following code in the header to redirect to my domain name. WHAT IS HAPPENING IS AS YOU CAN GUEST IT is going into a loop. WHAT I am trying to do is get the LINK juice SERP to the domain name. SO the java code is right but I want it to quit ONCE after the redirect / forward. as the site is in FRAME once directed to the domain name it STICKS....on the domain name. <script language='javascript'> document.location='http://www.GOOGLE.COM/'; </script> I am new VERY new to javascript Hi, I have three buttons on my site. When a button is clicked it pulls down a javascript pop up questionaire box. When the person closes the box they're redirected to Paypal depending on which button they clicked, hence the 3 if else options toward the end of this script. How might the following code be rewritten so that the pop up box no longer exists, but so the options still function and redirect correctly to Paypal? Code: <script type="text/javascript"> function popup(option){ Modalbox.show('<div><p>How did you learn about Dinosaur Pop?</p> <textarea id="message" name="message" rows="4" cols="30"></textarea><br><br><input type="button" value="Send" onclick="Modalbox.hide(); redirect(' + option + ');" /> or <input type="button" value="No, leave it!" onclick="Modalbox.hide(); redirect(' + option + ');" /></div>', {title: "Question", width: 300}); return false; } function redirect(option) { var messageObj = document.getElementById("message"); if (messageObj != null && messageObj.value != "") { var url = 'submit_message.php?message=' + encodeURIComponent(messageObj.value); new Ajax.Request(url, { method: 'get', onSuccess: function(transport) { } }) } if (option == 1) document._xclick.submit(); else if (option == 2) document._xclick1.submit(); else if (option == 3) window.location = "download/Dinosaur Pop Book.pdf"; } </script> Hi all, I'm new here and need some help. I don't know Javascript at all. I have a paypal form that looks like this: Code: <FORM action="https://www.paypal.com/cgi-bin/webscr" method="post"> <INPUT type="hidden" name="cmd" value="_s-xclick"> <INPUT type="hidden" name="hosted_button_id" value="VMMCF6CUGBPAN"> <INPUT type="hidden" name="on0" value="DATA">DATA<BR> <SELECT name="os0"> <OPTION value="250" selected="selected">DATA</OPTION> <OPTION value="400">DATA</OPTION> <OPTION value="1000">DATA</OPTION> </SELECT> <INPUT type="hidden" name="currency_code" value="USD"> <BR> <INPUT type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><IMG alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"> </FORM> This form works perfectly with my system now. I'm trying to add one fourth option that will redirect to another page, but I'm not sure if changing the name of the select will break the paypal transaction. So I tried this solution: Code: <FORM action="https://www.paypal.com/cgi-bin/webscr" method="post"> <INPUT type="hidden" name="cmd" value="_s-xclick"> <INPUT type="hidden" name="hosted_button_id" value="VMMCF6CUGBPAN"> <INPUT type="hidden" name="URL" value="DATA">DATA<BR> <SELECT name="URL" onchange="window.location.href= this.form.URL.options[this.form.URL.selectedIndex].value"> <OPTION value="250" selected="selected">DATA</OPTION> <OPTION value="500">500 Words</OPTION> <OPTION value="1000">1000 Words</OPTION> <OPTION value="project.html">1000+ Words</OPTION> </SELECT> <INPUT type="hidden" name="currency_code" value="USD"> <BR> <br> <INPUT type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><IMG alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"> </FORM> It works great if I select the fourth option with a internal url in it. But if I choose one of the other options it tries to dynamically change to a page "250", "500", or "1000" and they don't exist. Also, I'm not sure if changing the select name or the option value will break the transaction with paypal. What I'd like the form to do is nothing on all options except the fourth; which is a redirect. The first, second, and third option require the used to click a paypal buy now button. Can't I just take this... Code: onchange="window.location.href= this.form.URL.options[this.form.URL.selectedIndex].value" ...put it in the option tag and change it to onSelect or something? This is as far as I can go. I copied the code and altered it. Can anyone here help please? i need help with this code why isnt it redirecting me to my other webpage heres the code
Code: <form name="loginform"> <label>User name</label> <input type="text" name="usr" placeholder="username"> <label>Password</label> <input type="password" name="pword" placeholder="password"> <input type="submit" value="Login" onSubmit="validateForm();" /> </form> <script> function validateForm() { var un = document.loginform.usr.value; var pw = document.loginform.pword.value; var username = "username"; var password = "password"; if ((un == username) && (pw == password)) { window.location = "main.html"; return false; } else { alert ("Login was unsuccessful, please check your username and password"); } } </script> This is what I tried but alas, it does not work por que no? This is located on my main index file which if the result of z is greater than 1, I would have the user stay at this page as opposed to redirecting to the portrait design. I have optimized the website for mobile use so there is a reason of having two different pages, the wide screen has extra tools / other stuff that isn't shown on the portrait not because of size/responsive design but becasue I want it to be this way. Anyway... "What's the situation captain?" This is located above my <style> tag Code: <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="JavaScript" type="text/javascript"> <script> function redirect() { var x = screen.width; var y = screen.height; var z = x/y; if (z<1) { window.location.replace("alternate destination"); }; else ( ){ // stay here }; }; </script> Reply With Quote 12-21-2014, 01:58 AM #2 Old Pedant View Profile View Forum Posts Supreme Master coder! Join Date Feb 2009 Posts 28,311 Thanks 82 Thanked 4,754 Times in 4,716 Posts You have extra semicolons in there. In general, don't put a semicolon directly after a right brace }; There are a couple of exceptions, but they don't apply here. What's the point in having the else?? Hi there, I hope I post this in the right section. I'm trying to do a redirect using the below code: <script type="text/javascript"> window.location.href = "http://google.com" </script> FF and IE work as they should. Chrome doesn't. The request above to http://google.com, gets a 'canceled' status in Chrome browser > Development tools > "Network". I've tried several other functions: location.href = url location.replace(url) document.location = url location.assign(url) window.open(url, '_self') Same code pasted within a local html file works fine. Below is the redirect request that it's canceled by chrome: http://pastebin.com/hD36M1RG Any clues? Thanks Hi all I am creating a shopping cart in HTML & JavaScript. It's very nearly complete but I've hit a brick wall with trying to delete a single item. Where I list the shopping cart I have a delete button by every item. Here is the code to show that: Code: var buttonnode3= document.createElement('input'); // Create a button for each item to delete buttonnode3.setAttribute('type','button'); buttonnode3.setAttribute('name','Delete'); buttonnode3.setAttribute("value","Delete('"+ItemName[j]+"');"); buttonnode3.setAttribute("onclick","Delete('"+ItemName[j]+"');"); // for FireFox. This parses through the item name (DelName=item name to delete) buttonnode3.onclick = function() {Delete(ItemName[j]);}; // for Internet Explorer. Again, this parses through DelName tr.appendChild(buttonnode3); (The value name is for testing purposes). As you can see here, it shows fine But when I click delete, I get an 'undefined' error Here is the code for the Delete(); function Code: function Delete(DelName){ // Create function Delete "DelName" parses through item name alert(DelName); // testing for (var ind=0; ind<ItemName.length; ind++) { // Loop through array if(ItemName[ind] == DelName){ // Find item to be deleted ItemName.splice(ind,1); // Delete first record of item name Quantity.splice(ind,1); // Delete first record of its quantity Sum.splice(ind,1); // Delete first record of its sum alert('You have removed ' + DelName); // Alert user document.getElementById("div1").innerHTML=""; // Clear the whole shopping cart document.getElementById("div2").innerHTML=""; document.getElementById("div3").innerHTML=""; ViewShoppingCart(); // Once the cart is cleared, there may still be other items so update it again } } } I've tried doing this var DelName = ItemName[j]; and then change the buttonnode to show DelName and it works however DelName always gets set to the last Item in the array. For example, you delete Dinosaur but it deletes Firemans hat. Can anyone help me out of this hole? |