2

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.

1
  • what are you trying to do here? if you want to send file to server side script (in this case asp) use ajax. you are giving asp page as source where it is expected js (because you use type = javascript) Commented May 9, 2013 at 8:39

2 Answers 2

4

You can simply do this:

var id = $("[id$='hfProductIdList']").val();
$('head').append('<script type="text/javascript" src="https://test/tr.aspx?orderid=' + id + '" />');
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

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.