Roshanck Roshanck - 1 year ago 103
HTML Question

how to set a variable inside script tag

I have a page name index.php. And I have a script variable as follows at the top of the page:

var search_quarry = "some_test";

At the bottom of the same page I want to add this variable into the
attribute of a script tag:

<script src=NEED_TO_ADD_HERE></script>

This doesn't work:

<script src=search_quarry> </script>

Can anyone please tell me how to do this?

Answer Source

You'd need to do with DOM manipulation:

var search_query = 'some_test';
s = document.createElement('script');
s.src = search_query;

or, as an alternative, though I'm not sure if this'd work:

<script id="fixme"></script>
<script type="text/javascript">
    var search_query = 'some_test';
    document.getElementById('fixme').src = search_query;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download