How to change the src of a <script> using jQuery

Asked

Viewed 681 times

1

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

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

translating, on the first screen I access modal and external css and javascript files are accessed by 2 levels above ../../ and on the second screen 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 get one $("#pesquisaCliente).dialog("open"); the attr modify the href link increasing a folder level ../ have managed to understand?

  • The paths must begin with / to be related to Origen (root). Then it becomes easier to work and you don’t need to ../../. Have you tried that?

  • 4

    It doesn’t make much sense to change src from a script tag. The old script is not downloaded from memory, and I don’t even know if the new one would be loaded.

  • 6

    It gives me a strong impression of being a XY problem.

  • I understand Bacco, I’m wanting to talk about the solution instead of the problem, my problem and the following, I have a customer search modal that is accessed by two screens, customers and reports that have different folder levels, it turns out that at the client level the css and javascript links work perfectly, but at the reporting level they are not found, being found only one level above ../../../

  • 2

    And you can’t just put the respective CSS in the source of each modal?

  • I don’t understand, can you show me an example ?

  • You’d have to see how you’re doing in the practical case, otherwise it’s difficult. Why don’t you edit the question and show relevant excerpts of how the load does in the real case, and how is a modal of these? So the whole community can help. Remember that you know how so things there, and we only know what is written in the question.

  • 1

    Climbing back up the directory tree is usually the path to madness... ;)

Show 3 more comments

1 answer

3


One way to solve it 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>

That then no matter which page will open, the script will always be loaded correctly. There is another alternative. You can omit the domain and protocol and start the path with "/" that gives in it:

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

This has the advantage of being more portable.

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

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

Note that this variable caminho, and the structure containing it is defined at another point.

Or, with Sharepoint:

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

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.