Search code examples
javascriptgoogle-chromegoogle-chrome-extensiongoogle-chrome-app

Google Chrome Extension Redirect different page


I have popup.html I want to load different html page in extension when user click href. Example user will enter username and password and if they are correct login.html redirect to content.

Tried this:

window.onload=function(){
$( '#Login' ).click(function() { 
    alert('test');  
        chrome.browserAction.setPopup({
            popup:"login.html"
            });
        
});
}

Login is a button.But login.html didnt show up. Alert is working.


Solution

  • To change the popup html page you can use

    chrome.browserAction.setPopup
    

    more detailes on this page http://developer.chrome.com/extensions/browserAction.html

    you can do something like this:

    in html:

    <a onclick="changePopup();" href="#"> change popup</a>
    

    in js:

    function changePopup(){
        chrome.browserAction.setPopup({
           popup:"second_page.html"
        });
    }
    

    You didn't specified where is the link placed? in the popup page or in an html page, so I assumed the linked is placed in popup html page.

    If the link is placed outside extension, you should use Content Scripts https://developer.chrome.com/extensions/content_scripts.html

    UPDATE: You should put the java-script file in your manifest.json using background pages or event pages depending on what you need.

    Here you can find more info:

    https://developer.chrome.com/extensions/background_pages.html

    http://developer.chrome.com/extensions/event_pages.html

    ....
      "background": {
        "scripts": ["background.js"]
      },
    ....
    

    If you loaded the javscript file from your html page using the <script> tag , you should use the Message passing api : http://developer.chrome.com/extensions/messaging.html to interact with your html.