Issue
I'm currently creating a Chrome Extension that has a popup.html and in that pop up is 2 things. An input field and Submit button.
The Input field takes a EXTN number from a user and when submitted is pressed I need to then navigate the user to a new tab appending the Input from the user to the end.
Input = 9999 when submit is pressed user is taken to: Submit = http://name.com/9999
I have this working with when just use the HTML file but it doesn't seem to do anything when loaded into Chrome as an Extension.
Here is my code for the input and onClick:
Popup.HTML
<script src="popup.js"></script>
<input type="text" id="page" />
<input type="submit" value="submit" onclick="goToPage();" />
popup.js
function goToPage() {
var page = document.getElementById('page').value;
window.location = "http://name.com/" + page; }
I understand that you can't run in line JS so I've called it out from the popup.js file but its still not working.
Solution
You can't use inline JS in Chrome extension (the onclick="goToPage();"
in the input element is considered inline Javascript) . You have to put all your Javascript code in a separate file.
You will need to use chrome.tabs API to open a new tab. For example:
popup.html
<input type="text" id="page" />
<button id="submit">Submit</button>
<script src="popup.js"></script>
popup.js
document.getElementById('submit').onclick = function(){
var page = document.getElementById('page').value;
chrome.tabs.create({url:'http://name.com/' + page});
}
Answered By - Iván Nokonoko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.