0

I have a HTML page .In my head section i have below code

    <link href="/Content/css/Stylesheet1.css" rel="stylesheet" type="text/css">
    <link href="/Content/css/jquery.datepick.css" rel="stylesheet" type="text/css">
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery/jquery.datepick.js" type="text/javascript"></script>
    <script src="/Scripts/jquery/Script.js" type="text/javascript"></script>

How do I change the file path names of the above HTML elements to the below one using jquery dynamically

<link href="http://localhost:9090/Content/css/Stylesheet1.css" rel="stylesheet" type="text/css">
<link href="http://localhost:9090/Content/css/jquery.datepick.css" rel="stylesheet" type="text/css">
<script src="http://localhost:9090/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="http://localhost:9090/Scripts/jquery/jquery.datepick.js" type="text/javascript"></script>
<script src="http://localhost:9090/Scripts/jquery/Script.js" type="text/javascript"></script
7
  • Why do you want to change the path of it through Javascript ? You can use relative path instead. Commented Dec 23, 2014 at 8:37
  • @ShaikhMohammedShariq He asks for a chair , you answer for a banana. what if he wants to send this html file to someone in other domain ??? Commented Dec 23, 2014 at 8:38
  • @RoyiNamir its because he might need a banana instead of a chair. We need to first understand the root problem. Commented Dec 23, 2014 at 8:40
  • yes i am trying to take a HTML page using cross domain concept Commented Dec 23, 2014 at 8:40
  • @SunilGeorge I guess you do need a chair. ( as you asked for). Commented Dec 23, 2014 at 8:40

1 Answer 1

4
$("link[href^='/Content/css']").attr('href', function(i, oldhref) {
    return 'http://localhost:9090' + oldhref;
});
$("script[src^='/Scripts']").attr('src', function(i, oldsrc) {
    return 'http://localhost:9090' + oldsrc;
});
Sign up to request clarification or add additional context in comments.

3 Comments

This should do the work for you. Here is the documentation link for the answer provided by @Barmar. api.jquery.com/attribute-starts-with-selector Starts with Selector is the perfect option.
i tried the code got an error in console"Uncaught Error: Syntax error, unrecognized expression: link[href^=/Content/css]"
It needed some extra quoting because of the / characters. Try it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.