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

jquery - Javascripthtml keep url params - Stack Overflow

programmeradmin2浏览0评论

say I provide someone with a link to my page that looks like the following

www.linkone?something=ten

When they visit the page, the param will be visible within the url for that page. If on that page I then have a link like so

<a href="linktwo.html"> Link Two </a>

When they click on it, Link Two's url will look like

www.linkone/linktwo.html

Is there any way to add the param from the first link onto any other links within the page? So when they visit Link Two, I want to url to end up being

www.linkone/linktwo.html?something=ten

I know it is probably possible via javascript, but the homepage has a lot of links on it to other pages within the site, and I need to param on all links. What is the best way to achieve this?

Thanks

say I provide someone with a link to my page that looks like the following

www.linkone.?something=ten

When they visit the page, the param will be visible within the url for that page. If on that page I then have a link like so

<a href="linktwo.html"> Link Two </a>

When they click on it, Link Two's url will look like

www.linkone./linktwo.html

Is there any way to add the param from the first link onto any other links within the page? So when they visit Link Two, I want to url to end up being

www.linkone./linktwo.html?something=ten

I know it is probably possible via javascript, but the homepage has a lot of links on it to other pages within the site, and I need to param on all links. What is the best way to achieve this?

Thanks

Share Improve this question asked Oct 14, 2015 at 11:15 katie hudsonkatie hudson 2,89313 gold badges54 silver badges101 bronze badges 5
  • 1 What if the new link itself has new parameters or you want to delete an old parameter? What then? – Niet the Dark Absol Commented Oct 14, 2015 at 11:17
  • I have tried local storage, but not too sure I trust this method. I have also tried a javascript solution which gets the param and then sets the attr value of my link with this param appended, but when you have a lot of links this seems very inefficient. – katie hudson Commented Oct 14, 2015 at 11:18
  • p.s. the parameters will always be the same for each user, they will never change – katie hudson Commented Oct 14, 2015 at 11:18
  • A short javascript snippet can modify all a elements when the page loads and append the querystring - you would not need to do them individually – Alex K. Commented Oct 14, 2015 at 11:18
  • stackoverflow./questions/901115/… – caramba Commented Oct 14, 2015 at 11:18
Add a ment  | 

6 Answers 6

Reset to default 7
$('a:not([href=#])').on('click', function (e) {
    e.preventDefault();
    location.href = this.href + "your param";
});

My suggestion is:

$('a:not([href=#])').click(function(e) {
  e.preventDefault();

  // Getting query-string parameters from current page URL.
  var query = location.search.substr(1);

  // Getting URL from current link;
  var url = new URL($(this).attr('href'));

  // Updating query-string parameters of current link.
  url.search += (url.search.indexOf('?') == 0 ? '&' : '?') + query;

  // Getting the link target.
  var target = $(this).attr('target');

  // Navigating to new URL.
  if (target) {
    window.open(url.toString(), target);
  } else {
    location.assign(url.toString());
  }
});

One way to achieve a similar function is to use cookies or session. When a user visits your site with additional param, set a cookie on the parent page with that param so that it would be available for other calls/links. You could do that in session as well. session would retain the value till the session times out and cookie expires based on the expiry date set for it.

Link to Cookie Reference

You can update anchors href attributes at Document Ready. Below code solution for current HTML querystrings carries over the selected anchors href attributes.

var selectedAnchors = $("a");

selectedAnchors.each( function () {

    var itemHref = $(this).attr("href");

    if ( itemHref.indexOf("?") > -1 )
        itemHref += "&";
    else
        itemHref += "?";

    itemHref += location.search.substr(1);

    $(this).attr("href", itemHref );

});

Please try this. Just replace word "something" with your own parameter.

Thanks

$("a").click(function(e){
    e.preventDefault();
    var a = $(this).attr('href');
    var something = getUrlParameter('something');
    var newURL = a+"?something="+something;
    window.location.href = newURL;
});

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

HTML:

<a href="http://google./">Google</a>

This is possible (with php):

<a href="http://www.linkone./linktwo.html?something=
<?php echo $_GET["something"]; ?>"> Link Two </a>
发布评论

评论列表(0)

  1. 暂无评论