简体   繁体   English

您能否从上一页的链接中执行某些onload功能

[英]Can you perform certain onload functions from a link on a previous page

I'm not even sure what I'm thinking of will work and can't seem to find the right wording to get and search results that are remotely helpful so here goes. 我什至不知道我在想什么会起作用,而且似乎找不到正确的措辞来获得和搜索结果,这对远程帮助非常有用,因此请继续。

What I want to be able to do is have a link from one page then cause the linked page to display a certain way. 我想要做的是从一个页面获得一个链接,然后使链接的页面以某种方式显示。 The code below is the script being used on the page I'll be linking to. 下面的代码是我将链接到的页面上正在使用的脚本。

 $(document).ready(function() {
            $('.hideshow').click(function () {
                var name = $(this).attr('id').replace("-L","");
                if ($(this).hasClass("hidden")) {
                    $(this).addClass("shown");
                    $(this).removeClass("hidden");
                    $('div#' + name).show(500);
                } else {
                    $(this).addClass("hidden");
                    $(this).removeClass("shown");
                    $('div#' + name).hide(500);
                }
            });
        });

This code will hide or show content when links on the page are clicked using the id names used in the body of the file. 使用文件正文中使用的ID名称单击页面上的链接时,此代码将隐藏或显示内容。 Now what I want to be able to do is have the link from the previous page indicate certain links on this page as being shown. 现在,我想要做的就是让上一页的链接指示该页面上的某些链接,如图所示。 The following is some of the in body code. 以下是一些体内代码。

<a class="hideshow hidden" id="cat-articles-L" style="cursor:pointer;"><font style="font-size:24px; color:#06F; text-decoration:underline;"> Cat Articles</font></a><br />
                   <div id="cat-articles" style="display:none;">
                   &nbsp;&nbsp;&nbsp;&nbsp;<a class="hideshow hidden" id="cat-beds-L" style="cursor:pointer;"><font style="font-size:18px; color:#06F; text-decoration:underline;">Cat Beds</font></a><br />

On default the "Cat Articles" are visible but "Cat Beds" is hidden until "Cat Articles" is clicked, then there could be sublevels so more items under "Cat Beds" The idea is when you link from the other page having it load with certain items already open. 默认情况下,“ Cat Articles”可见,但“ Cat Beds”是隐藏的,直到单击“ Cat Articles”,然后可能会有子级别,因此“ Cat Beds”下有更多项。这个想法是当您从另一页加载时某些商品已经打开。 Hope I made this clear enough, still new to this site and posting questions. 希望我对此做的足够清楚,仍然是本网站的新内容并发布问题。

Add a parameter to the link on the original page. 将参数添加到原始页面上的链接。 Something like domain.com/page.html?id=5 and then have javascript check for a QueryString of id and do something like a switch with it. 诸如domain.com/page.html?id=5 ,然后让javascript检查id的QueryString并执行诸如开关的操作。

See How can I get query string values in JavaScript? 请参阅如何在JavaScript中获取查询字符串值? for how to get QueryStrings if you aren't familiar. 了解如何在不熟悉的情况下获取QueryStrings。

If you need to pass multiple of the same type (example, multiple IDs), you can make an array and pass that array to the other page. 如果您需要传递多个相同类型的示例(例如,多个ID),则可以创建一个数组并将该数组传递到另一页。 In that array, include every id you want to display. 在该数组中,包括要显示的每个ID。

See an example of an array to url here: JavaScript Array to URLencoded 在此处查看要发送网址的数组的示例: JavaScript到URLencoded的数组

Example (Original Page): 示例(原始页):

var idArray = new Array(1,5,15,38);
var url = "http://www.blah.com/link.html?"+idArray.join('&');

Example (Linked page): 示例(链接页面):

var qs = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
        var p=a[i].split('=');
        if (p.length != 2) continue;
        b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
})(window.location.search.substr(1).split('&'));
var idArray = qs["id"];
for (var i = 0; i < idArray.length; i++) {
    switch(idArray[i]){
        case 1:
            //show link1
            break;
        case 2:
            //show link3
            break;
        default:
            //do nothing, but required
            break;
    }
}

Note: It may look that I'm using JQuery, but I'm not. 注意:看起来我在使用JQuery,但我没有。 This will work without it just fine. 没有它就可以了。

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

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