Check if Javascript script exists on page

后端 未结 7 754
深忆病人
深忆病人 2021-02-02 10:16

I have a bookmarklet that I\'ve made and it loads a script from my server onto the users current page. However I have an if check in my script that if a condition is not met the

7条回答
  •  花落未央
    2021-02-02 10:49

    You can place id attributes on your script tags and use document.getElementById('your-id') to identify whether the script is on the page before adding.

    if (!document.getElementById('your-id')) {
        // append your script to the document here, ensure it has its id attribute set to 'your-id'
    }
    

提交回复
热议问题