Marv
Marv

Reputation: 138

Reference to jQuery.js in background_page causes jQuery not working

I defined jQuery.js in manifest.json under content_scripts

"background_page": "html/bg.html",
"content_scripts": [
    {
        "matches": ["\u003Call_urls\u003E"],
        "js": ["js/jquery.js"/]
    }
]

and in bg.html, I added a click event handler to find <p>...</p> nodes

<script>

    chrome.browserAction.onClicked.addListener(function(tab) {  
        chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
    });

</script>

It works fine if just doing this way. But if I add a js reference in the bg.htm, jQuery then not working anymore, even src=""

<script type="text/javascript" src="../js/jquery.js">

    chrome.browserAction.onClicked.addListener(function(tab) {  
        chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
    });

</script>

The background_page and the content_script should be in different scopes, still not find what's going on here.

Upvotes: 0

Views: 282

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220136

If your script tag has a src attribute, the contents are not parsed.

See here: http://jsfiddle.net/cnK7s/


Instead, use 2 separate script tags:

<script type="text/javascript" src="../js/jquery.js"></script>

<script type="text/javascript">

    chrome.browserAction.onClicked.addListener(function(tab) {  
        chrome.tabs.executeScript(null, {code: "alert($('p').text());"});
    });

</script>

Upvotes: 2

Related Questions