Cannot change html script src with external javascript
Ad
I have read every single google result I could find, and this still is not working... all i'm trying to do is change a script source depending on a link that is clicked. The source files will have one or two lines returned by document.write, like so:
document.write(`This is content script a`);
So I have the following setup, an html file with two links that call the changeSrc function, which SHOULD change the source:
<!DOCTYPE html>
<html>
<body>
<a id="aLink" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" onclick="changeSrc('a');return false;">Change to a</a/><br>
<a id="aLink" target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" onclick="changeSrc('b');return false;">Change to b</a/><br>
<script id="contentScript" src="a.js"></script>
</body>
</html>
The JS file:
function changeSrc (arg) {
document.getElementById("contentScript").innerHTML = '';
if (arg == "a") {
document.getElementById("contentScript").innerHTML = '<script id="contentScript" src="a.js"></script>';
} else if (arg == "b") { {
document.getElementById("contentScript").innerHTML = '<script id="contentScript" src="b.js"></script>';
}
}
Why is the script source not changing on the link click?!
Thank you for your time, I feel like an absolute idiot...
Ad
Answer
Ad
Try
document.getElementById("ContentScript").src = your_script_source_here
Hope this helps!
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad