M C Anand

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromM C Anand
ToMe
SubjectHow to pass value (URL) from a HTML file to an another HTML file ?
Date20 June 2007 20:13
Hi,

I have a problem in passing value to an another html file. Here is my
requirement:

1. I want to open an existing html file when I click on a specified link.

2. While in the new window which was opened, I would like to use META
refresh tag. The page would be re-directed to a link/or a file after the set
time.

3. But this re-direction link should be passed from the original file. How
should I pass this re-direction URL to the other HTML file.

4. Because this re-direction will vary on different files. And, I will have
only one intermediate set to do this.

I would like to do this using javascript.

How should i pass this URL value to the new html file ?

Appreciate your help in resolving my query

Thanks and Regards
Anand
FromMe
ToM C Anand
SubjectRe: How to pass value (URL) from a HTML file to an another HTML file ?
Date23 June 2007 13:36
Anand,

> I would like to do this using javascript.

JavaScript is the wrong thing to do this with. It is much better to use a
server side programming language. You could link to this:
<a href="http://server/page1.php?nextpage=http%3A//server/page2.php">

Then page1.php could contain this:
<?php

$nextpage = $_GET['nextpage'];

if( $nextpage && preg_match("/^https?:\/\//i", $nextpage ) ) {
  print '<meta http-equiv="Refresh" content="0;url='.
    .htmlspecialchars($nextpage);
    '">";
}

?>

Note that it is essential to include the protocol check and
htmlspecialchars, as they prevent cross site scripting attacks.

> 2. While in the new window which was opened, I would like to use META
> refresh tag. The page would be re-directed to a link/or a file after the
> set time.

If you still feel it is better to use JavaScript (the whole thing will then
fail if the user has JavaScript disabled), then you should not use META
Refresh, because browsers generally don't like to create META tags with
scripts - the tag will often just be ignored. Instead, it is better to set
location.href after a specified timeout.

It is possible to pass the new URL in the ?foo part of the address if
needed, but JavaScript does not offer a convenient way to retrieve the GET
variables from the address, so you would have to write it yourself, and add
the security check to make sure it starts with http:// or https:// (to
ensure it is not a scriptable URL like javascript: or vbscript: or tcl:
etc.).

An easier alternative is to store the second URL in a global variable on the
first page. The popup, when it loads, can then check the opening page to get
the contents of the variable, and then it can set the timeout ready to load
the new URL.

Use a link like this on the main page:
<a href="FileToDownload.whatever" onclick="dopop(this);return false;">
Then have this script:
var secondlink;
function dopop(oLink) {
  secondlink = oLink.href;
  window.open('intermediate.html','_blank','width=500,...');
}
Then intermediate.html can have this script:
var newURL = opener.secondlink;
setTimeout(function () {
  location.href = newURL;
},5000); //5 second delay

(The way I have used the link here even allows it to work in browsers
without JavaScript, where they will open the file directly, without opening
a popup.)

> Because this re-direction will vary on different files

This sounds like a script that you will use to trigger downloads, in which
case, I have to give you one last warning. Internet Explorer will not start
a download unless the user actually clicked a link or submitted a form to
start it on the intermediate page. If this script is used to start a
download, IE will refuse to start it, and instead it will show a security
warning saying that your site tried to start a download without permission.
If this is going to be a problem, then you should create a link for them to
click, instead of setting a timeout or META refresh.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.