javascript - auto click button after window.location.href - Stack Overflow

I have an onclick event on my anchor tag that redirects the user to a specific page and auto clicks on

I have an onclick event on my anchor tag that redirects the user to a specific page and auto clicks on a link after the loading of the page.

this is what i have right now:

var experience_modal = "#experienceModal";

    function experienceModal() {

        window.location.href = "/Resume";
        $(experience_modal).trigger('click');

    };
<p style="margin-bottom: 10px;"><strong><a href="javascript:void(0);" onclick="experienceModal()">Add</a> an Experience</strong></p>



    

I have an onclick event on my anchor tag that redirects the user to a specific page and auto clicks on a link after the loading of the page.

this is what i have right now:

var experience_modal = "#experienceModal";

    function experienceModal() {

        window.location.href = "/Resume";
        $(experience_modal).trigger('click');

    };
<p style="margin-bottom: 10px;"><strong><a href="javascript:void(0);" onclick="experienceModal()">Add</a> an Experience</strong></p>



    

I am encountering a problem that the trigger property does not work after the redirecting to the specific page.

please guide me on how to fix this problem.

thank you in advanced!

Share Improve this question edited Feb 15, 2017 at 4:40 Sreetam Das 3,4092 gold badges24 silver badges36 bronze badges asked Feb 15, 2017 at 3:45 TerenceTerence 3521 gold badge6 silver badges19 bronze badges 1
  • once the page is redirected the javascript will not run anymore, this won't work – cansyrup756 Commented Feb 15, 2017 at 3:50
Add a ment  | 

4 Answers 4

Reset to default 3

On page load/reload all js execution stops. That's why your trigger mand does not run after assigning a new URL to location.href. You need to get the pages 'municate' each other somehow.

One way would be passing a query string in URL :

Code on the page that the user supposed to click "Add an experience" :

window.location.href = "/Resume?showExpModal";

Code on Resume page :

$(document).ready( function() {
  // existing code
  // ...
  if(window.location.href.split('?').pop() == 'showExpModal') {
    $(experience_modal).trigger('click');
  }
}

window.location.href triggers a new page load, which means that any additional code on the current page will not be exceuted.

I guess one option would be to redirect to the current page, with an added query string, that would then trigger some conditional code. But that seems a bit convoluted, I'd suggest rethinking your strategy instead.

Try executing your code before leaving the page..?

Try this

var experience_modal = "#experienceModal";

function experienceModal() {
    setTimeout(function() {
       window.location.href = "/Resume";
    })
    $(experience_modal).trigger('click');

};

You can use sessionStorage or cookies to hold on to some information that you can access on the next page.

/***/ On the previous page /***/
sessionStorage.setItem('click-this-button-after-page-loads', '#thisButton');

/***/ On the new page /***/
let buttonToBeClicked = sessionStorage.getItem('click-this-button-after-page-loads');

// Click the button here
document.querySelector(buttonToBeClicked).click();

// Remove the key from session storage
sessionStorage.removeItem('click-this-button-after-page-loads');

The session storage goes away when the browser is closed. You can read more on the MDN: https://developer.mozilla/en-US/docs/Web/API/Window/sessionStorage

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163856a4614527.html

相关推荐

  • javascript - auto click button after window.location.href - Stack Overflow

    I have an onclick event on my anchor tag that redirects the user to a specific page and auto clicks on

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信