1

I want to do something like this:

            $("#startButton").click(function() {
                <?php if(somecondition){ ?>
                    <script src="some-external-js-srouce"></script>
                    <noscript><img src="irrelevant"></noscript>
<?php } ?>

});

Meaning that I want to execute some external javascript inside a jquery function if a button has been clicked. Can you help me?

  • `` is not JavaScript.... you have to bit more clear about what you are trying to do. I understand that you might want to load some external script, but what you want to do with that image tag? – Felix Kling Oct 14 '11 at 09:41
  • 1
    Your condition is serverside, but your button click is clientside. Basicly how would it ever change on the clients end? See http://stackoverflow.com/questions/610995/jquery-cant-append-script-element – Marco Johannesen Oct 14 '11 at 09:41
  • The condition is not a problem. It's just a script i got from some tracking i need to insert if the button is being clicked. –  Oct 14 '11 at 10:26

3 Answers3

0

You're putting HTML inside JavaScript. That's not quite possible.

You probably want $.getScript to load and execute a script dynamically at runtime. It does not support <noscript> but you're using JavaScript for it anyway, so that does not really make sense to me...

pimvdb
  • 151,816
  • 78
  • 307
  • 352
0

if you want to execute a javascript function from an external file then you can try like this -

<script type="text/javascript" src="myjs.js">
// code that will refer functions of myjs.js

</script>
saarthak
  • 1,004
  • 2
  • 12
  • 26
  • It's unreliable to combine a ` – pimvdb Oct 14 '11 at 09:43
0

use jQuery.getScript( url, function(){});

http://api.jquery.com/jQuery.getScript/

sathis
  • 547
  • 1
  • 3
  • 21