2

functions of background.js file not invoked by onclick() method in popup.html. Is there any wrong with my manifest.jjson file? because normally that funtion work properly.

manifest.json

{
        "name": "Popping Alert",
        "description": "http://stackoverflow.com/questions/15194198/background-js-not-working-chrome-extension",
        "background": {
            "scripts": [
                "background.js"
            ]
        },
        "content_scripts": [
        {
          "matches": ["http://www.google.com/*"],
          "css": ["print.css","styles.css"],
          "js": ["background.js"]
        }
      ],
        "version": "1",
        "manifest_version": 2,
        "browser_action": {
            "default_title": "Click Me",
            "default_icon": "hello.png"
        }
    }

popup.html

<script type="text/javascript" src="background.js"></script> 
<section id="content">
                <ul class="column">
                    <!--eqblock-->
                    <li>
                        <section class="block">
                                    <a href="#"><img src="images/facebook.jpg" alt=""  /></a> 
                                        <h2><a href="#" onclick="MyFunction1()">Facebook </a> </h2> 


                        </section>
                    </li>
        </ul>
        </section>

background.js

function MyFunction1 () {
                                 localStorage.setItem("facebook", "https://www.facebook.com/");
                                window.open(localStorage.getItem("facebook"));
                                alert("ok");
                                        }
Sadegh
  • 865
  • 1
  • 23
  • 47
Rajitha Perera
  • 1,581
  • 5
  • 26
  • 42

1 Answers1

3

You can't call functions from background.js from non-background scripts directly. their contexts are different. You need to use Message Passing

Prakash GPz
  • 1,675
  • 4
  • 16
  • 27