How can I create a javascript badge or widget?

前端 未结 5 1234
鱼传尺愫
鱼传尺愫 2021-01-14 09:38

I want to create a javascript badge that displays a list of links. We host the javascript on our domain. Other sites can put an empty div tag on their page and at the botto

5条回答
  •  礼貌的吻别
    2021-01-14 09:49

    I would give the SCRIPT tag an ID and replace the script tag itself with the DIV + contents, making it so they only have to include one line of code. Something along the lines of the following:

    
    

    In your script, you can swap out the SCRIPT tag for your DIV in one fell swoop, like so:

    var scriptTag, myDiv;
    scriptTag = document.getElementById('my-script');
    myDiv = document.createElement('DIV');
    myDiv.innerHTML = '

    Wow, cool!

    '; scriptTag.parentNode.replaceChild(myDiv, scriptTag);

提交回复
热议问题