{"id":693,"date":"2022-02-16T17:44:34","date_gmt":"2022-02-16T22:44:34","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=693"},"modified":"2022-02-16T17:44:36","modified_gmt":"2022-02-16T22:44:36","slug":"setting-up-src-dynamically","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/setting-up-src-dynamically\/","title":{"rendered":"Setting up src dynamically"},"content":{"rendered":"\n<p>Suppose you need to call some JavaScript code on your page such as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;script src\"somecode.js\">&lt;\/script><\/code><\/pre>\n\n\n\n<p>However, the somecode.js is different based on other parameters on your page. So you would need to pass a variable rather than the static string to the src. If you just replace somecode.js, with a variable name, you code will not be called.<\/p>\n\n\n\n<p>The following example shows how you can manipulate DOM to define src so that your code runs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script id=\"mycode\"&gt;&lt;\/script&gt;\n&lt;script type=\"text\/javascript\"&gt;\n\/\/ dynamic source is defined by a previous javascript code on the page\ndocument.getElementById('mycode').src = dynamicsource;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you need to call some JavaScript code on your page such as: However, the somecode.js is different based on other parameters on your page. So you would need to pass a variable rather than the static string to the src. If you just replace somecode.js, with a variable name, you code will not be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/693\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}