recording
js stitching url
For example, sometimes we need to achieve for a jump button, you can use the following way to do:
function ReturnIndex() {
var rex = RegExp("tools")
var url = window.location.origin
var new_url = "http://127.0.0.1:"+window.location.port
if (url.match(rex)) {
curr_url = window.location.origin // 获取根网址:比如:https://www.baidu.com
a = curr_url.split(".")[0]
now = a.split("//")[1]
window.location.href = curr_url.replace(now, "www")
} else {
console.log(new_url)
window.location.href = new_url
}
};
$("#ReturnBtn").on('click', function () {
ReturnIndex()
});
- Description:
- All information acquisition URL:
window.location
Have methods and properties, I am here to blog Park, for example, by
F12
commissioning in the console:ancestorOrigins: DOMStringList {length: 0} assign: ƒ assign() hash: "" host: "i-beta.cnblogs.com" hostname: "i-beta.cnblogs.com" href: "https://i-beta.cnblogs.com/posts/edit" origin: "https://i-beta.cnblogs.com" pathname: "/posts/edit" port: "" protocol: "https:" reload: ƒ reload() replace: ƒ () search: "" toString: ƒ toString() valueOf: ƒ valueOf() Symbol(Symbol.toPrimitive): undefined __proto__: Location
Seen from above, and here I use mainly
origin
,port
,href
this property Sa, respectively请求源
,端口
,超链接URL
.
- url stitching, because I am here is to jump from a second-level domain domain name, so use a replacement string:
- First definition includes
tools
regular:var rex = RegExp("tools")
- Then made a statement [judgment here because of convenient local and remote debugging service], if the current url contains two domain names
tools
, is replaced by a domain namewww
, then the next button to jump back to a domain resources; if not, it means local environment, jump to theip+port
next page.
- First definition includes
- All information acquisition URL:
Assigned to the specified attribute
Use 选择器
+ attr
approach to the assignment.attr(attributeName: string, value: string | number)
$(".qrcode_modal").attr('src', new_url) // 给src赋值
Above is given class
to qrcode_modal
the label src
property assignment new_url
.