baros
baros

Reputation: 251

dynamically value inside javascript src url

I'd like to put a javascript variable value dynamically inside to a script src url.

<head runat="server">
 <script type="text/javascript">
    var id = $("[id$='hfProductIdList']").val();
 </script>
 <script type="text/javascript" src="https://test/tr.aspx?orderid=id">
 </script>    
</head>

If there is a way to set up these value dynamicly It would be great for me.

Upvotes: 2

Views: 4541

Answers (2)

palaѕн
palaѕн

Reputation: 73906

You can simply do this:

var id = $("[id$='hfProductIdList']").val();
$('head').append('<script type="text/javascript" src="https://test/tr.aspx?orderid=' + id + '" />');

Upvotes: 4

Marek Lewandowski
Marek Lewandowski

Reputation: 3401

You need to append it using pure javasript;

var id = $("[id$='hfProductIdList']").val();
var jsElem = window.document.createElement('script');
jsElem.src = 'https://test/tr.aspx?orderid=' + id;
jsElem.type = 'text/javascript';
$('head').append(jsElem);

Also it might be better idea to append it to body rahter than head, but it's not a subject of your question.

Upvotes: 2

Related Questions