简体   繁体   中英

Find server name from CSS href with jQuery

How can I get the following server path assigned to a variable with jQuery or JavaScript ?

<link rel="stylesheet" href="https://myServer:44301/Some/Path/">

From

<link rel="stylesheet" href="/core/assets/styles.min.css">
<link rel="stylesheet" href="/Content/Site.css">
<link rel="stylesheet" href="https://myServer:44301/Some/Path/">
<link rel="stylesheet" href="https://anotherServer/Another/Path/">

The string Some/Path is what needs to be matched on.

To be clear, the variable should contain the string https://myServer:44301/

You can try this:

function getHostWithPath(path) {
    var linkNodeList = document.getElementsByTagName('link');
    for (var i = 0 ; i <linkNodeList.length ; i++) {
        var l = document.createElement("a");
        l.href = linkNodeList[i].href;
        /*
        // Usefull properties available in l:
        console.log(l.hostname);
        console.log(l.port);
        console.log(l.pathname);
        console.log(l.hash);
        console.log(l.protocol);
        console.log(l.search); // Query String
        */
        if (l.pathname == path) {
            var re = new RegExp(path + '.*');
            return l.href.replace(re, '');
        }
    }
}

var myHostname = getHostWithPath('/Some/Path');

This can be done by JQuery by running the following code on your page:

var server = $("link[href*='/Some/Path']").attr("href").replace("Some/Path/", "");

What it does is it select the element whose href contains /Some/Path and gets the whole href attribute's text/value. Then it removes the Some/Path/ part leaving you with only https://myServer:44301/ , which is then set in the variable server .

Snippet:

 var server = $("link[href*='/Some/Path']").attr("href").replace("Some/Path/", ""); alert(server); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <link rel="stylesheet" href="/core/assets/styles.min.css"> <link rel="stylesheet" href="/Content/Site.css"> <link rel="stylesheet" href="https://myServer:44301/Some/Path/"> <link rel="stylesheet" href="https://anotherServer/Another/Path/"> 

 $(function() { $("link").each(function() { var href = $(this).prop("href"); alert(href); }); }); 
 <!DOCTYPE html> <html> <head> <title>title here</title> <meta charset="utf-8" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link rel="stylesheet" href="/core/assets/styles.min.css"> <link rel="stylesheet" href="/Content/Site.css"> <link rel="stylesheet" href="https://myServer:44301/Some/Path/"> <link rel="stylesheet" href="https://anotherServer/Another/Path/"> </head> <body> <!-- page content --> </body> </html> 

The code below take each link and display them using alert() . Depending on your condition, I let you wrap the alert() with a condition bloc if() to take the right href (maybe your links are always set up like this, so you'll want to take the 3rd, etc...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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