0

I'm trying to create a redirect page but I need to use two parameters to create the address link.

<!DOCTYPE html>
<!--
Test file
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<meta http-equiv="refresh" content="0;url=link" />-->
</head>
<body>
<p>Redirecting...</p>
<script language="javascript">
   var chapterx = document.getElementById("chapter").value;
   var linex = document.getElementById("line").value;
   var link = "http://www.mypage.com/help?chapter=" + chapterx + "," + linex;

   //window.prompt(link);
   window.location = link;
</script>
</body>
</html>

I'm testing this by loading the page from my PC, not from the server.

I have very basic concept about HTML and JS and I can't figure out what I'm doing wrong.

I read something from Redirect from an HTML page to create that code.

Plus, theres any way to write the 'link' variable before redirect to see what happen?

Also I have Firebug installed but I cant found the variables that I declared to see their status.

Community
  • 1
  • 1
E_Blue
  • 1,021
  • 1
  • 20
  • 45
  • What should the values of `chapterx` and `linex` be and where do you get them? – Lucio Sep 27 '15 at 23:52
  • @Lucio both are numbers, parameters in the address bar. – E_Blue Sep 28 '15 at 00:07
  • 1
    Then you just need to get them from the URL and not from the DOM. This will help you out since the rest of the code is ok: http://stackoverflow.com/q/5448545/1505348 – Lucio Sep 28 '15 at 00:12

2 Answers2

1

Your code is correct. But it will not work because JavaScript detects an error on that line:

var chapterx = document.getElementById("chapter").value;

You don't have any element on your page with the chapter id. Your next line is erroneous too because there is no element on your page with the line id.

I added this to your code:

<div id="chapter"></div>
<div id="line"></div>

after <p>Redirecting...</p> and it successfully redirected me to: http://www.mypage.com/help?chapter=undefined,undefined

Hopefully that helped?:)

  • Thanks, I understand now, I'm calling something non-existent. I'm trying to set the values that I have in the address bar to create the new address. My old page looks like www.old.com/help.hmtl?chap=1&lin=32 and the new address is www.new.com/help.hmtl?chapter=1,32 – E_Blue Sep 28 '15 at 00:13
0

Reading the link posted by Lucio I make the following code

<!DOCTYPE html>
<!--
Test file
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<meta http-equiv="refresh" content="0;url=link" />-->
</head>
<body>
<p>Redirecting...</p>
<script language="javascript">
   var chapterx = decodeURIComponent(window.location.search.match(/(\?|&)chap\=([^&]*)/)[2]);
   var linex = decodeURIComponent(window.location.search.match(/(\?|&)lin\=([^&]*)/)[2]);
   var link = "http://www.myweb.com/help.html?chapter=" + chapterx + "," + linex;
   window.location = link;
</script>
</body>
</html>
E_Blue
  • 1,021
  • 1
  • 20
  • 45