Solved

Immediately redirect

Posted on 2004-10-19
21
662 Views
Last Modified: 2011-09-20
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
Comment
Question by:daveask
  • 6
  • 5
  • 5
  • +1
21 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12352981
Hi daveask,

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

Regards,
Zyloch
0
 

Author Comment

by:daveask
ID: 12353066
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
 
LVL 1

Expert Comment

by:HillBox
ID: 12353069
Write as a javascript function and the use onload event in your body tag, like <body onload="doRedirect();">

Hope it help!
0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 40 total points
ID: 12353090
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
 
LVL 13

Accepted Solution

by:
StormyWaters earned 40 total points
ID: 12353092
>> 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
 
LVL 1

Assisted Solution

by:HillBox
HillBox earned 30 total points
ID: 12353101
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
 

Author Comment

by:daveask
ID: 12353107
HillBox,

Somebody has told me "onload" means after loading all code including pictures.....
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12353111
@Stormy,

I'm curious to know whether the scripts in the <head> or the <body> are executed first lol
0
 
LVL 1

Expert Comment

by:HillBox
ID: 12353117
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12353124
@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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Expert Comment

by:StormyWaters
ID: 12353125
Argh, I need a way to slow down my browser so I can test this. :P
Will get back to you...
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12353148
Waiting for Stormy to confirm,

But I believe head is executed first, inside body second, and as expected, onload last
0
 
LVL 1

Expert Comment

by:HillBox
ID: 12353158
@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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12353162
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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12353173
Head does execute first.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12353181
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
 
LVL 1

Expert Comment

by:HillBox
ID: 12353183
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
 

Author Comment

by:daveask
ID: 12353244
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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12353286
What browser was the person using?
0
 

Author Comment

by:daveask
ID: 12353347
Sorry, I need to check my log file again and I will do some test as well........
0
 

Author Comment

by:daveask
ID: 12357620
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now