Results 1 to 10 of 10

Thread: redirect not working

  1. #1
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default redirect not working

    hi all

    i want to show a redirect message for 5 second before redirecting person to yahoo website.

    i using the following script. but its not redirecting just a mesage is displayed on click.

    Code:
    <script language="JavaScript"> 
    function redirect(){ 
    window.location="http://www.yahoo.com"; 
    document.write("You will be redirected to yahoo in 5 sec.");
    } 
    </script>
    on link
    Code:
    <a href="#" onclick="setTimeout('redirect()', 5000)"> Click to go to yahoo</a>
    thanks
    vineet

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you document.write to the page after it has loaded, it wipes out everything on and about the page. Even if that were not true and the rest of it worked, when the timeout fires, you would go right to Yahoo without seeing the message. Try:

    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    document.body.appendChild(d);
    } 
    </script>
    Code:
    <a href="#" onclick="redirect(); return false;"> Click to go to yahoo in 5 seconds</a>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default redirect not working

    Quote Originally Posted by jscheuer1 View Post
    If you document.write to the page after it has loaded, it wipes out everything on and about the page. Even if that were not true and the rest of it worked, when the timeout fires, you would go right to Yahoo without seeing the message. Try:

    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    document.body.appendChild(d);
    } 
    </script>
    Code:
    <a href="#" onclick="redirect(); return false;"> Click to go to yahoo in 5 seconds</a>
    hi jscheuer1

    i am using IE7 and tried it.
    I m only able to redirect to yahoo but not able to see any message before redirecting.
    I have expanded the delay from 5 sec to 10 sec/20 sec but still no message is displayed.
    The hyperlink is in between my content. so on click i want all page content should become blank and only the redirect message should be displayed.

    vineet

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var t = document.body.getElementsByTagName('*'), i,
    d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    for (i = t.length - 1; i > -1; --i)
    t[i].style.display = 'none';
    document.body.appendChild(d);
    } 
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default redirect not working

    Quote Originally Posted by jscheuer1 View Post
    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var t = document.body.getElementsByTagName('*'), i,
    d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    for (i = t.length - 1; i > -1; --i)
    t[i].style.display = 'none';
    document.body.appendChild(d);
    } 
    </script>
    hi john

    thanks. its working now.
    just wanted to ask is it possible that i can place this script before my link in my <td> so that message should be displayed in my page.

    actually with this script my header is also vanishing.

    if this is not posible then tell me how can apply styles to this div. i know how to make classes in stylesheet but just tell where should i put my class name for this particular div in which mesage is displayed.

    vineet
    Last edited by vineet; 11-27-2008 at 08:29 AM. Reason: miss something

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, you said:

    on click i want all page content should become blank and only the redirect message should be displayed
    But, if you have some thing(s) that you want to remain displayed, you can mark them with a class, for example 'remain':

    HTML Code:
    <div id="header" class="remain">
    Whatever
    </div>
    then in the script:

    Code:
    for (i = t.length - 1; i > -1; --i)
    if(!/remain/.test(t[i].className))
    t[i].style.display = 'none';
    Just make sure that the parent of that element is either the body or also is marked class="remain".
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default redirect

    Quote Originally Posted by jscheuer1 View Post
    Well, you said:



    But, if you have some thing(s) that you want to remain displayed, you can mark them with a class, for example 'remain':

    HTML Code:
    <div id="header" class="remain">
    Whatever
    </div>
    then in the script:

    Code:
    for (i = t.length - 1; i > -1; --i)
    if(!/remain/.test(t[i].className))
    t[i].style.display = 'none';
    Just make sure that the parent of that element is either the body or also is marked class="remain".

    hi john

    marking everything with a new class "remain" wil make everything confusing as i m not using <div>. i m using <table> and those already have classes applied and its not posible to mark every table with "remain".

    tell me can i apply class on this particular <div> instead so that after applying style on this <div> it looks better. at present its mixing with my background and not proper readable. it wil solve our purpose.

    vineet

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can give as many class names to an element as you like:

    HTML Code:
    <table class="mytable remain">
    But tables are not such a good idea. Also, you could create a division for either keeping things displayed in or for removing the display from, and alter the script code. I'd say like put everything that you want to disappear in:

    HTML Code:
    <div id="killthis">
    stuff to remove here.
    </div>
    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    document.getElementById('killthis').style.display = 'none';
    document.body.appendChild(d);
    } 
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default redirect

    Quote Originally Posted by jscheuer1 View Post
    You can give as many class names to an element as you like:

    HTML Code:
    <table class="mytable remain">
    But tables are not such a good idea. Also, you could create a division for either keeping things displayed in or for removing the display from, and alter the script code. I'd say like put everything that you want to disappear in:

    HTML Code:
    <div id="killthis">
    stuff to remove here.
    </div>
    Code:
    <script type="text/javascript"> 
    function redirect(){ 
    setTimeout(function(){window.location.href='http://www.yahoo.com/';}, 5000);
    var d = document.createElement('div');
    d.appendChild(document.createTextNode('You will be redirected to yahoo in 5 sec.'));
    document.getElementById('killthis').style.display = 'none';
    document.body.appendChild(d);
    } 
    </script>

    hi john

    in this case as u say to do. i wil have to give id to the message div also. and then call it in my <td> by getElementbyID. Because at present it is displaying at the top of the page where browser starts.

    vineet

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm assuming now that you can arrange things to work out the way you want them, right?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •