使用jQuery在页面加载时将字符串添加到url? [英] Add string to url on page load with jQuery?

查看:75
本文介绍了使用jQuery在页面加载时将字符串添加到url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面加载时将此特定字符串添加到我的网址末尾:

I'm trying to add this specific string to the end of my url on page load:

?aa_campaign = f45632

?aa_campaign=f45632

http://examplesite.com/test.html

用于营销和跟踪。

我试过这个:

if window.location.href.indexOf("http://examplesite.com/test.html") {
  window.location = "http://examplesite.com/test.html?aa_campaign=f45632";
}

直接上升不起作用,但这个想法是我正在寻找的对于。有什么想法?

That straight up didn't work but the idea is what I'm looking for. Any thoughts?

推荐答案

不需要 jQuery ,你可以这样做在大多数现代浏览器中使用纯 JavaScript ,即:

No need for jQuery, you can do this with pure JavaScript in most "modern" browsers, i.e.:

if (window.location.href  === "http://examplesite.com/test.html") {
    window.history.pushState("object or string", "Title", "http://examplesite.com/test.html?aa_campaign=f45632");
}

pushState()方法


pushState()接受三个参数:状态对象,标题(当前忽略
)和(可选)URL。让我们更详细地检查每个
这三个参数:

pushState() takes three parameters: a state object, a title (which is currently ignored), and (optionally) a URL. Let's examine each of these three parameters in more detail:

state object — The state object is a JavaScript object which is associated with the new history entry created by pushState(). Whenever

用户导航到新状态时,会触发popstate事件,
事件的state属性包含历史条目的
状态对象的副本。

the user navigates to the new state, a popstate event is fired, and the state property of the event contains a copy of the history entry's state object.

The state object can be anything that can be serialized. Because Firefox saves state objects to the user's disk so they can be restored

我们在状态对象的序列化表示上强加了640k
个字符的大小限制。如果
将序列化表示形式大于
的状态对象传递给pushState(),则该方法将抛出异常。如果你需要比这更多的
空间,我们鼓励你使用sessionStorage和/或
localStorage。

after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage.

title — Firefox currently ignores this parameter, although it may use it in the future. Passing the empty string here should be safe

以防止将来对方法进行更改。或者,您可以为您要移动的州通过
短标题。

against future changes to the method. Alternatively, you could pass a short title for the state to which you're moving.

URL — The new history entry's URL is given by this parameter. Note that the browser won't attempt to load this URL after a call to

pushState()之后,浏览器不会尝试加载此URL,但稍后可能会尝试加载URL,例如用户重新启动浏览器后的
。新网址不一定是
绝对值;如果是相对的,则相对于当前URL进行解析。
新网址必须与当前网址的来源相同;否则,
pushState()将抛出异常。此参数是可选的;如果未指定
,则将其设置为文档的当前URL。

pushState(), but it might attempt to load the URL later, for instance after the user restarts the browser. The new URL does not need to be absolute; if it's relative, it's resolved relative to the current URL. The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. This parameter is optional; if it isn't specified, it's set to the document's current URL.

SRC: https://developer.mozilla.org/en-US/docs/Web/指南/ API / DOM / Manipulating_the_browser_history

这篇关于使用jQuery在页面加载时将字符串添加到url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆