Skip to main content

How can I make a redirect page in jQuery/JavaScript?


How can I redirect the user from one page to another using jQuery?



Source: Tips4allCCNA FINAL EXAM

Comments

  1. jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

    It is better than using window.location.href =, because replace() does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.

    For example:

    // similar behavior as an HTTP redirect
    window.location.replace("http://stackoverflow.com");

    // similar behavior as clicking on a link
    window.location.href = "http://stackoverflow.com";

    ReplyDelete
  2. Simply do :

    var url = "http://stackoverflow.com";
    $(location).attr('href',url);

    ReplyDelete
  3. It would help if you were a little more descriptive in what you are trying to do. If you are trying to generate paged data, there are some options in how you do this. You can generate separate links for each page that you want to be able to get directly to.

    <a href='/path-to-page?page=1' class='pager-link'>1</a>
    <a href='/path-to-page?page=2' class='pager-link'>2</a>
    <span class='pager-link current-page'>3</a>
    ...


    Note that the current page in the example is handled differently in the code and with CSS.

    If you want the paged data to be changed via AJAX, this is where jQuery would come in. What you would do is add a click handler to each of the anchor tags corresponding to a different page. This click handler would invoke some jQuery code that goes and fetches the next page via AJAX and updates the table with the new data. The example below assumes that you have a web service that returns the new page data.

    $(document).ready( function() {
    $('a.pager-link').click( function() {
    var page = $(this).attr('href').split(/\?/)[1];
    $.ajax({
    type: 'POST',
    url: '/path-to-service',
    data: page,
    success: function(content) {
    $('#myTable').html(content); // replace
    }
    });
    return false; // to stop link
    });
    });

    ReplyDelete
  4. This works for every browser:

    window.location.href = 'your_url';


    Good luck!

    ReplyDelete
  5. $jq(window).attr("location","http://google.fr");


    This version works well with jQuery 1.6.2.

    ReplyDelete
  6. var url = 'asdf.html';
    window.location.href = url;

    ReplyDelete
  7. You can do that without jQuery as:

    window.location = "http://yourdomain.com";


    And if you want only jQuery then you can do it like :

    $jq(window).attr("location","http://yourdomain.com");

    ReplyDelete
  8. I think it is elegant to build a form with GET method and then submit it, and it will work in every browser and every version.

    < form id="getter" method="GET" action="URL" >
    < input type="hidden" name="variable" value="value" />
    < /form >


    and you go:

    $( '#getter' ).submit();

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.