[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

Immediately redirect

Hi Experts,

I got page1 as following. What I want is "if ( some conditions) then immediately redirect to page2". However, I found the whole page1 will be sent to the client before redirect to page2.
My question is how to redirect to page2 as soon as possible whthout waiting the whole page1 to be sent to the client?

<% asp code
%>
<html>
<head>
<SCRIPT language="javascript">
<!-- if ( some comditions ) {go to page2;}
//-->
</SCRIPT>
</head>
<body> html code
</body>
</html>
0
daveask
Asked:
daveask
  • 6
  • 5
  • 5
  • +1
3 Solutions
 
ZylochCommented:
Hi daveask,

Can your condition be mirrored in ASP? If so, you can use Response.Redirect...

Regards,
Zyloch
0
 
daveaskAuthor Commented:
Hi Zyloch,

In fact that is also what I am thinking!!
I want to redirect if my page is framed by other site.
Do you know any ASP code doing that?
0
 
HillBoxCommented:
Write as a javascript function and the use onload event in your body tag, like <body onload="doRedirect();">

Hope it help!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZylochCommented:
Unfortunately no, I don't know any method to do that.

The best I can come up with is

if (top.location!=self.location) {
   self.location=top.location;
}

but that is what you're doing right now, right?
0
 
StormyWatersCommented:
>> Write as a javascript function and the use onload event in your body tag, like <body onload="doRedirect();">

No, that will wait for the page to load. If you put it right after the <body> tag I think the code with be interpretted before the rest of the body loads and therefore redirect. Don't know for sure though, and can't test atm.
0
 
HillBoxCommented:
Let so that in your page so ... if your page is trapped into any frame, it will do redirect to its original url:
<body><head>
<script lanaguage="JavaScript">
     function doRedirect()
     {
       if (top.location != location) {
        top.location.href = document.location.href ;
       }
     }
</script>
</head>
<body onLoad="doRedirect();">
your page content
</body></html>
0
 
daveaskAuthor Commented:
HillBox,

Somebody has told me "onload" means after loading all code including pictures.....
0
 
ZylochCommented:
@Stormy,

I'm curious to know whether the scripts in the <head> or the <body> are executed first lol
0
 
HillBoxCommented:
And if you don't want to use onLoad , you can also call this javascript function after your body tag.  I think it should work.
0
 
ZylochCommented:
@Hillbox

Nope, onload will wait until EVERYTHING is loaded. It's good for things where you're not sure whether something is loaded or not so you don't want to get an object expected error.
0
 
StormyWatersCommented:
Argh, I need a way to slow down my browser so I can test this. :P
Will get back to you...
0
 
ZylochCommented:
Waiting for Stormy to confirm,

But I believe head is executed first, inside body second, and as expected, onload last
0
 
HillBoxCommented:
@Zyloch and @daveask

Yes, you are right.  Onload will run after everything is loaded.  So I think just call javascript function righ after body tag.
0
 
StormyWatersCommented:
Putting an inline script (no functions) in both the <head> and the <body> redirects the browser before loading the page. Tested with an extremely large image off of a slow server, so it's definitely working.
Head seems slightly faster, but I haven't specifically tested that.
Tested in IE6 and Firefox 0.9.3
0
 
StormyWatersCommented:
Head does execute first.
0
 
ZylochCommented:
Lol. Nice, heh. I'm pretty sure head is fastest. See below:

<html>
<head>
<script language="javascript">
<!--

alert('Head wins as expected');

//-->
</script>
</head>
<body onload="alert('Last as expected');">
<script language="javascript">
<!--

alert('Second is the best');

//-->
</script>
</body>
</html>
0
 
HillBoxCommented:
I just test and this should work fine, put this inside your <head> tag
<script lanaguage="JavaScript">
       if (top.location != location) {
            top.location.href = document.location.href ;
       }
</script>
0
 
daveaskAuthor Commented:
That is very strange, please check my original question, I did put javascript in <head>!!!
I think the whole page was sent to the client because I found the bytes transfored is the all bytes for the whol page. I found that in my log file (IIS).
0
 
StormyWatersCommented:
What browser was the person using?
0
 
daveaskAuthor Commented:
Sorry, I need to check my log file again and I will do some test as well........
0
 
daveaskAuthor Commented:
Hi Experts,

After checked and tested I recognised that we have to send whole page/code before we can redirect!
Use Zyloch's example, only the first alert
alert('Head wins as expected');
will apear on the screen, but the rest code need to be sent to the client as well. They are just ingnored by the browser and without being displayed. I think that is the way a Client-side works.
I checked the log file again and made a test page to run in my PC to check the log file and found all the bytes for the whole page were sent to the client.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now