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.
3 Answers
Reset to default 3It 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条)