javascript - Chrome Extension: Create a new tab by click a button - Stack Overflow

I am a beginner building a Chrome extension. I have an issue using the function described in the Chrome

I am a beginner building a Chrome extension. I have an issue using the function described in the Chrome extension developer doc to make a button to create a new tab in "popup.html". It doesn't work no matter which methods I have tried from Stack Overflow. My code is as follows:

<html>
<head>
<title>Facebook Connect For Chrome Extension</title>
<script type="text/javascript" src="background.js"></script>
<script type="text/javascript" src="popup.js"></script>
<script>
function showIndex(){
   var index_url="/index.html",
   chrome.tabs.create({
   url: index_url
   }),
   }
</script>
<body>
<button value="tab" style="width:100px; height:100px;" onclick="showIndex();">Go to  Index</button>
</body>

or

function createTab() {
    chrome.tabs.create({url: "/index.html"});
}
<a href="#" onclick="creatTab();">Go to Index</a>

Neither option seems to work.

So I wonder whether this function should be placed in background.js? If not, please tell me what's wrong with this code. Thanks in advance!

BTW I changed the URL to www.stackoverflow. It is still the same---not working.

I am a beginner building a Chrome extension. I have an issue using the function described in the Chrome extension developer doc to make a button to create a new tab in "popup.html". It doesn't work no matter which methods I have tried from Stack Overflow. My code is as follows:

<html>
<head>
<title>Facebook Connect For Chrome Extension</title>
<script type="text/javascript" src="background.js"></script>
<script type="text/javascript" src="popup.js"></script>
<script>
function showIndex(){
   var index_url="/index.html",
   chrome.tabs.create({
   url: index_url
   }),
   }
</script>
<body>
<button value="tab" style="width:100px; height:100px;" onclick="showIndex();">Go to  Index</button>
</body>

or

function createTab() {
    chrome.tabs.create({url: "/index.html"});
}
<a href="#" onclick="creatTab();">Go to Index</a>

Neither option seems to work.

So I wonder whether this function should be placed in background.js? If not, please tell me what's wrong with this code. Thanks in advance!

BTW I changed the URL to www.stackoverflow.. It is still the same---not working.

Share Improve this question edited Sep 8, 2013 at 1:48 HansUp 97.2k11 gold badges80 silver badges137 bronze badges asked Sep 8, 2013 at 1:19 CrathinkerCrathinker 11 silver badge1 bronze badge
Add a ment  | 

3 Answers 3

Reset to default 3

It looks like you spelled create wrong in your HTML.

Your issue is probably that Chrome does not allow you to use "unsafe" code in extensions. See the documentation here. You cannot have javascript in your html. You have to subscribe to the event handler on the DOM element.

<html>
<head>
<title>Facebook Connect For Chrome Extension</title>
<script type="text/javascript" src="background.js"></script>
<script type="text/javascript" src="popup.js"></script>
<body>
<button id="index" value="tab" style="width:100px; height:100px;">Go to  Index</button>
<script type="text/javascript" src="indexStuff.js"></script>
</body>
</html>

You then need a new indexStuff.js file with this

function showIndex() {
    var index_url = "/index.html";
    chrome.tabs.create({
        url: index_url
    });
}

document.getElementById('index').addEventListener("click", showIndex);

Note, the script tag could be moved to the top if you add an event handler to check when the DOM is loaded.

function showIndex(){

   var index_url="/index.html",//why are you using "," instead of ";"?

   chrome.tabs.create({
       url: index_url
   }), //why are you using "," instead of ";"?
}

why are you using "," at the end of line, instead of ";"?

You can use window.open(url, title, options) to open a popup window via JavaScript.

options is a string containing one or more of these variables (or empty):


width width of the window

height height of the window

location URL visible or not

status statusbar visible or not

menubar menubar visible or not

directories I'm guessing this is the bookmark bar

toolbar toolbar (back, home, etc.) visible or not

resizable whether or not resizable

scrollbars whether or not to enable scrollbars


e.g.:

window.open('http://website./popup.html', 'Popup Window', 'width=640,height=480,location=yes,scrollbars=yes');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225551a4617444.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信