0

i read many documents about this but i couldnt figure out that. I need help about execute another js when button clicked. test.js is my javascript file to execute. And in that file there is a just one line code like "alert("test.js executed!");

In my javascript file my click function is like that.

$("#src").click(function(){
    alert("hi");
});

It's working well, i need to execute my "test.js" script. Like this:

$("#src").click(function(){
    alert("hi");
    $.getScript("javascript/test.js",function(){

    })
    .success(function(){
        alert("success");
    })
    .error(function (){
        alert("Failed to execute script");
    });
});

But nothing happens. No errors aswell. How can i execute that script with click ?

2
  • What exactly is it that you need to run in test.js? If it's just a function, then rather import test.js on your page and call that function directly in the click listener. Commented Feb 3, 2016 at 9:48
  • No not just a function, there a many codes includes functions too. Commented Feb 3, 2016 at 10:14

1 Answer 1

1

That would fall under the use of eval, since that's exactly what getScript does.

This is not allowed by the default Chrome extension CSP and for a good reason.

You could argue that if the script is included with your extension, the security risk is non-existent,and you can override the CSP to allow that. It's bad and sloppy though.

Ideally, you want to simply include test.js with a <script> tag into the page, and call some function in it instead. Might need some modification on the file, but this is the proper way to do it.

If you absolutely need to execute the whole test.js every time (though there is no reason for it), create and append a <script> element with src appropriately set. This does not violate the CSP.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.