How to change the src of a script using jQuery

1

How can I change the value of the href attribute of a <link> and src of a <script> .

Real problem, I have two screens that are in different levels (different folders), in which both are accessed by the window.load that is within the modal in option Open .

In the first screen I access the modal and the external css and javascript files are accessed by 2 levels above ../../ and in the second screen they are accessed with three levels ../../../

Example:

jQuery:

$("#pesquisaClienteCss").attr("href","../../view/css/pesquisaCliente.css");

html:

<link id="pesquisaClienteCss" href="../../view/css/pesquisaCliente.css" rel="stylesheet" type="text/css" >

I need when I have a $("#pesquisaCliente).dialog("open"); attr modify the href of the link increasing a level of folder ../ could understand?

    
asked by anonymous 31.10.2014 / 17:35

1 answer

3

One way to solve is to put all your important scripts in a known path, and use absolute paths. For example, instead of:

<script src="../foo/bar.js"></script>

Use:

<script src="http://teusite.com/foo/bar.js"></script>

Thattherenomatterwhichpagewillopen,thescriptwillalwaysbeloadedcorrectly.Thereisanotheralternative.Youcanomitthedomainandtheprotocolandstartthepathwith"/" that gives the same:

<script src="/foo/bar.js"></script>

This has the advantage of being more portable.

Okay, it's not always possible. In these cases server side code is your friend. If you use some server side technology (Java, Ruby, ASP.NET, PHP ...) you can mount the script tag dynamically with the correct path. For example, in .NET you would do something like this:

<script src='<% #Eval("Caminho") %>'></script>

Note that this variable caminho , as well as the structure that contains it, is defined elsewhere.

Or, with SharePoint:

<script src="~sitecollection/foo/bar.js"></script>

Good luck!

    
31.10.2014 / 19:22