0

I have two different scripts that do different things but I do not want them to run at the same time as one script displays the video in black and white and another script lets the user add text on the video.

<script type="text/javascript" src="<?php echo base_url();?>application/scripts/canvas.js"></script>

<script type="text/javascript" src="<?php echo base_url();?>application/scripts/filter.js"></script>

I have made a 3rd script which will be used to chose the script depending on the button clicked and so far this is what I have below but nothing happens when I click on the button, Is the script loaded in the correct way when the blk button is clicked?

    var buttonClickedId = buttonElement.id;
    if( buttonClickedId === 'blk' ){
       // do btn1 stuff
       <script type="text/javascript" src="<?php echo base_url();?>application/scripts/filter.js"></script>
    }
    else if( buttonClickedId === 'btn2' ){
       // do btn2 stuff
       <script type="text/javascript" src="<?php echo base_url();?>application/scripts/canvas.js"></script>
    } 
    else{
       <script type="text/javascript" src="<?php echo base_url();?>application/scripts/filter.js"></script>
    }
  
  } 
lissettdm
  • 7,119
  • 1
  • 4
  • 23
  • Consider using JavaScript instead? `fetch` would work here – evolutionxbox Feb 03 '21 at 16:45
  • Yes, but not like that. You have to programmatically create a new ` – Randy Casburn Feb 03 '21 at 16:45
  • 2
    It seems like you have designed your scripts to immediately execute some code. Don't do that, design them to define functions instead. Then, at the click of a desired button you simply run the function you want to. As a further point, you can place those functions inside a single script. – El_Vanja Feb 03 '21 at 16:47
  • @evolutionxbox - fetch will not allow the script to be executed. The OP would have to read the text of the script and `eval` it. But even that is restricted by the default content-security policy as described [here](https://developer.mozilla.org/en-us/docs/Web/API/Fetch_API/Using_Fetch#feature_detection) – Randy Casburn Feb 03 '21 at 16:51
  • 2
    Thank you for all the replies, I think I'm better off amending my scripts in functions so I can do this easier and more efficiently. – Noobie1229 Feb 03 '21 at 16:51
  • @RandyCasburn sure fetch won't execute it, but it can be appended to the page which will run the JS code. – evolutionxbox Feb 03 '21 at 16:51
  • @evolutionxbox - no, it won't - eval is restricted when content is retrieved from an XMLHttpRequest object. – Randy Casburn Feb 03 '21 at 16:52
  • @RandyCasburn I'm getting mixed up https://stackoverflow.com/a/14521482/989920 – evolutionxbox Feb 03 '21 at 16:55
  • 3
    Does this answer your question? [Dynamically load JS inside JS](https://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js) – evolutionxbox Feb 03 '21 at 16:55
  • @evolutionxbox - 1. that link points to my exact suggestion in my very first comment to the OP, 2. That is not performed via XMLHttpRequest using `fetch()` as you suggested to do - the link you provided (and the answer to this question) uses HTTP directly to retrieve the src. – Randy Casburn Feb 03 '21 at 16:59
  • 1
    @RandyCasburn 1. I can change my mind. 2. your suggestion works, mine didn't. 3. profit? – evolutionxbox Feb 03 '21 at 17:02
  • 1
    No..this isn't about pride. It's about accuracy. I have been corrected and capitulated many times on here. You show that you are an upstanding person. – Randy Casburn Feb 03 '21 at 17:07
  • Nicely done, gentlemen. – Grumpy Feb 03 '21 at 17:10

0 Answers0