Reputation: 9293
Trying to dynamically change src
attribute for js
file.
<script id='mod' src="arts.js"></script>
<script src="main.js"></script>
inside arts.js
I have:
$(document).on('click', function(){
console.log('arts');
});
Inside banns.js
I have:
$(document).on('click', function(){
console.log('banns');
});
Inside the main.js
I have:
$(document).on('contextmenu', function(){
$('#mod').attr('src', 'banns.js');
});
After a right click I'm expecting the next click to write banns
but it doesn't work.
Is it possible (without reloading the page)?
Upvotes: 0
Views: 703
Reputation: 370729
Changing the src
of an existing script tag already in the DOM won't do anything. Instead, create another script tag, set its src
, and append it to the DOM.
This won't negate whatever the previous script tag did, though - if you want to disable the click listener added previously, you'll need to do so explicitly:
document.body.appendChild(document.createElement('script')).src = 'banns.js';
and, inside banns.js
, before setting the new click listener:
$(document).off('click');
Upvotes: 2