附加<a> links not working in Chrome but works through Notepad++?

时间:2017-01-10 04:08:03

标签: javascript html google-chrome google-chrome-extension hyperlink

So in popup.html I got some links that works when I load it into Chrome, but when I append some links using forminput.js; the links are appended and it does work when I launch it from Notepad++ but fails to work when I load it into Chrome, dropdown.js and createtab.js both works fine and has nothing to do with this problem, they contain logic for opening tabs and handling dropdown menu. Thanks

popup.html

<body>
<form id="aform">
  URL:<br>
  <input type="text" name="URL" id="URL">
  <br>
  Bookmark Name:<br>
  <input type="text" name="bookmarkname" id="bookmarkname">
  <br><br>
  <input type="button" id="formsubmit" value="Submit">
</form> 
<div class="dropdown">
  <button class="dropbtn" id="dropdown">Links</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="https://www.youtube.com">Youtube</a>
    <a href="https://www.amazon.com">Amazon</a>
    <a href="https://www.yahoo.com">Yahoo</a>
  </div>
</div>

<script src="forminput.js"></script>
<script src="dropdown.js"></script>
<script src="createtab.js"></script>

manifest.json

    {
    "name" : "Links",
    "version" : "1.0",
    "description" : "links 4 u",

    "manifest_version" : 2, 

    "browser_action": {
        "default icon":"icon.png",
        "default_popup":"popup.html"
    },

    "permissions": [
        "tabs",
        "storage"
    ]
}

forminput.js

    function retrieveFormData() {

            var URL = document.getElementById("URL").value;
            var Bookmarkname = document.getElementById("bookmarkname").value;
            var y = document.getElementById("myDropdown");
            var aTag = document.createElement('a');
            aTag.appendChild(document.createTextNode(Bookmarkname));
            aTag.href = URL;
            y.appendChild(aTag);
}


document.getElementById("formsubmit").addEventListener("click",retrieveFormData);

1 个答案:

答案 0 :(得分:0)

弹出窗口是一个单独的文档(请参阅extensions architecture),无法在扩展程序的内部网址之外导航,该网址是chrome-extension:// blablablabla /

如果您要在当前标签中打开链接(需要target="_blank"权限,或者更好的是{{3}),您需要在所有链接中添加"tabs"属性或使用tabs API }):

chrome.tabs.update({url: 'https://www.google.com'});