最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute a function after changing location.href - Stack Overflow

programmeradmin1浏览0评论

I want to call a function after redirecting the page.

Inside my function I put a parameter which is an ID but when I check the console it wont display. I know I can be able to run it via on click event but I wont get the ID param from the previous page.

Is there a way to get it done?

Code:

function encode(item_id){
  $('.js-encode').click(function(){
    var url = $(this).data('url');
    location.href = url; // new url
    save(item_id); // call function after new url finish loading
  });
}

function save(item_id){
  console.log(item_id); // check if there's item_id exists
}

I want to call a function after redirecting the page.

Inside my function I put a parameter which is an ID but when I check the console it wont display. I know I can be able to run it via on click event but I wont get the ID param from the previous page.

Is there a way to get it done?

Code:

function encode(item_id){
  $('.js-encode').click(function(){
    var url = $(this).data('url');
    location.href = url; // new url
    save(item_id); // call function after new url finish loading
  });
}

function save(item_id){
  console.log(item_id); // check if there's item_id exists
}
Share Improve this question edited Jun 9, 2016 at 3:54 Facundo Victor 3,5181 gold badge27 silver badges21 bronze badges asked Jun 9, 2016 at 3:13 claudiosclaudios 6,6568 gold badges50 silver badges97 bronze badges 11
  • 6 because you are redirected in another page. The javascripts in the previous page will not be applied to the new page – Anonymous Duck Commented Jun 9, 2016 at 3:16
  • Is there a way to get it? – claudios Commented Jun 9, 2016 at 3:18
  • 1 add it as query string, for example 'newlocation.?id=' + item_id + – Anonymous Duck Commented Jun 9, 2016 at 3:20
  • 3 All code below this line location.href = url is not executing at all because as soon as this line executes, new page starts to load. It's same as if you enter this URL in browser manually. – Ruslan Stelmachenko Commented Jun 9, 2016 at 3:22
  • 1 @Qwerty yes, JS virtual machine stops execution and browser starts to load new URL. I don't know how it is implemented internally in the JS VM, though. – Ruslan Stelmachenko Commented Feb 17, 2021 at 16:15
 |  Show 6 more ments

3 Answers 3

Reset to default 5

I agree with Katana's ment. Anything after your redirect statement will not run because the page itself is redirecting. One way that I would suggest to still get the item_id and get around that barrier, would be to include the item_id in the redirect url as a parameter. Then once on the new page, parse that parameter out of the url and save the item_id.

A great example from Cory Laviska's article on Parsing URLs in Javascript, shows how you can get the individual parameters from a URL.

Building onto Manish' answer:

Function on the current page:

function encode(item_id){
    $('.js-encode').click(function(){
    var url = $(this).data('url');
    location.href = url+'?saved_item_id='+item_id; // new url
    });
}

Function on the REDIRECTED Page (assuming you only have one parameter)

$( document ).ready(function() {
     var url = $(this).data('url');
     var item_id = url.queryKey['saved_item_id'];
     save(item_id);
 });

It may need a few tweeks because I didn't test the code, but hopefully it'll get you on the right track. :)

Hopefully this helps. If it helps and/or answers your question, please select as answer and up vote! :D Feel free to let me know if you have any questions

Try this one

The container is the section of your page where you perform an action.

function encode(item_id){
  $('.js-encode').click(function(){
    var url = $(this).data('url');
    $("#container").load(url,function(){
        // other stuffs and functionalities
         save(item_id); // call function after new url finish loading
    });
  });
}

You can try something like this.

function encode(item_id){
  $('.js-encode').click(function(){
    var url = $(this).data('url');
    location.href = url+'?saved_item_id='+item_id; // new url
    //save(item_id); // call function after new url finish loading
  });
}

/*(function save(item_id){
  console.log(item_id); // check if there's item_id exists
}*/

Further more , On New redirected URL you can get item_id which was appended to URL in previous page.

Hope this may help.

发布评论

评论列表(0)

  1. 暂无评论