简体   繁体   English

仅在单击特定链接后在页面加载后执行 JQuery

[英]Executing JQuery after page load only after clicking a specific link

I have a link in my app that when clicked, leads to another page.我的应用程序中有一个链接,单击该链接会转到另一个页面。 I want to execute some JQuery on this new page after it loads, but only if that specific link is clicked to get to the page .我想在加载后在这个新页面上执行一些 JQuery ,但前提是单击该特定链接以进入该页面

I have this JQuery:我有这个 JQuery:

    $('#new_to_topics').click(function(){
        $(document).ready(function() {
            $('#topic_guidelines').slideDown('normal');
            $('#topic_guidelines').addClass('on');
        });
    });

where #new_to_topics is the id of the link that leads to the new page and其中#new_to_topics是指向新页面的链接的 ID,并且

$('#topic_guidelines').slideDown('normal');
$('#topic_guidelines').addClass('on');

is the JQuery code I want to execute on that new page.是我想在那个新页面上执行的 JQuery 代码。 However, this does not work.但是,这不起作用。 How should I do this?我该怎么做?

You could pass a location hash to the new page, and then conditionally run some javascript based on that hash.您可以将位置 hash 传递到新页面,然后基于该 hash 有条件地运行一些 javascript。

If my link was to mynewpage.html#fromXlink (this would show in the address bar)如果我的链接指向mynewpage.html#fromXlink (这将显示在地址栏中)

My javascript on mynewpage.html could be:我在 mynewpage.html 上的 javascript 可能是:

$(document).ready(function() {
  if (location.hash == '#fromXlink') {
    $('#topic_guidelines').slideDown('normal');
    $('#topic_guidelines').addClass('on');
  }
});

You could add a variable to the query string ie somepage.aspx?fromthislink=true and then pick that up in jquery.您可以在查询字符串中添加一个变量,即 somepage.aspx?fromthislink=true,然后在 jquery 中选择它。

This shows how 这显示了如何

If it cam from that link then fire off your jquery.如果它来自该链接,则启动您的 jquery。

You can use window.name to pass data to the target page (although I would prefer passing data in the hash, if possible).您可以使用window.name将数据传递到目标页面(尽管如果可能,我更喜欢在 hash 中传递数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM