Create a javascript link or banner using another javascript

Asked

Viewed 400 times

-1

I wanted to put a banner or link in a particular location of the page using a javascript that would be in another location of the same page

guy

<script>
função que eu colocaria os links ou banners
</script>

<div>
<script>
local que apareceria os links e/ou banner (não sei se precisaria de script aqui)
</script>
</div>
  • You don’t need two tags script, does everything in one. And to appear the "links", would be in HTML, would be what you want?

  • it doesn’t have to be separate, it’s for a blog the script would be outside and within the posts I put a link with a modal and in this modal I would put this link equal to all posts... More if I want to change the link was just go in javascript..

1 answer

1


You will need the js function that adds the element or html you need:

 <script>
    function adicionarLink(){
        document.getElementById("div-inserir-link").innerHTML = "<a href='http://google.com'>Google</a>";
    };

    document.addEventListener('DOMContentLoaded', function () {
        adicionarLink();
    }, false);
 </script>

the onload function will only call the addition function, but you can do it in the onlick of some element, or at any other time.

And your html:

<div id='div-inserir-link'></div>
  • did not work no, I put the script anywhere else in the div did not appear the link =/

  • @Lary, I edited the answer. Test now. (=

  • now yes rsrs, thank you so much @Aline

  • What Douglas commented on in his post, that’s right. You should not have tags <script> spread across your code. Ideally, these js will be even in a file. js, not in the same file as HTML, imagine then about being spread all over HTML. It’s a pretty bad practice. You can read a little about it here: https://mauricioszabo.wordpress.com/2012/04/unobtrusive-javascript/

  • but in case it will be a script only that I will use and the div tags in the posts

Browser other questions tagged

You are not signed in. Login or sign up in order to post.