js stitching url and assign attributes for a tag html

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 F12commissioning 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, hrefthis 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 toolsregular: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 name www, then the next button to jump back to a domain resources; if not, it means local environment, jump to the ip+portnext page.

Assigned to the specified attribute

Use 选择器+ attrapproach to the assignment.attr(attributeName: string, value: string | number)

$(".qrcode_modal").attr('src', new_url) // 给src赋值

Above is given classto qrcode_modalthe label srcproperty assignment new_url.

Guess you like

Origin www.cnblogs.com/cpl9412290130/p/11961186.html