1

Whenever I open a page I am working on, the page makes a lots of amount of doms using javascript, and each one looks like this:

<span class="link img">text</span>

Let me say I assign one of them to a variable called a, for temporary.

I have another function that is to put every of them, including the a, an onclick event, which should be written in the HTML code page, so I can store the whole page and load it later.

The moment it attaches is before loading the doms into document body. Creating doms, and then this function attaches onclick on them, and then load them in the document body.


What it is supposed to do is making this a to:

<span class="link img" 
 onclick="function(){window.open('http://example.com')}">
     text
</span>

So clicking a opens a new window. I made a CSS so every 'link' class looks like a link anyway.


I tried contain one of these into the function to achieve the above change:

a.onclick = function(){window.open('http://example.com')};

,

a["onclick"] = function(){window.open('http://example.com')};

,

const openFunc = function(){
    window.open('http://example.com')
};
a["onclick"] = openFunc;

I don't know why, they don't attach onclick property on a.

Dinir
  • 171
  • 2
  • 10
  • http://stackoverflow.com/questions/7975708/how-to-add-click-event-to-anchor – yhabib Sep 24 '16 at 09:45
  • Please learn the proper vocabulary and google the difference between DOM vs DOM-Node and object-property vs Node argument. The problem is that you talk *(and probably search)* about different things than what you actually mean. And why do you want to replace proper links with spans? I hate it when I can't open a link in a new tab because of what you're about to do. So much that If I have to deal with that just a few times, I leave the page, and look for a different place to get what I want. It's a huge inconvenience and disruption in my browsing experience. – Thomas Sep 24 '16 at 10:00
  • @Thomas The page I am working on is running as a local file. So I really love to just use anchor tag to make a link but browser won't allow it. I understand what you feel about those things not looking like a link that actually open new windows, so I added proper CSS to them. – Dinir Sep 24 '16 at 10:02
  • The browser won't allow links? You'll have to explain that, because it blows my mind; it's one of the core features of a browser to follow links. – Thomas Sep 24 '16 at 10:07
  • Browser won't allow link to a local resource for security reasons. I tried the common anchor tags, and then tried 'window.open', and I got an error message `Not allowed to load local resource:` on the Chrome developer console. It felt a bit weird because what I tried to link is not a local file. https://twitter.com/DinirNertan/status/779203049315192832 – Dinir Sep 24 '16 at 10:08

3 Answers3

1

I didn't understand what you are saying but if you want to add a event on variable 'a' which have the object reference of an element: Use a event listener

a.addEventListener("click",funcNametocall,false);

read about it here http://www.w3schools.com/js/js_htmldom_eventlistener.asp

shahryar
  • 83
  • 5
0

Thomas' reply made me look back at the anchor tag once more time, and it awkwardly solved the problem.

Here's what I put in the function that is to make every one of the doms to a link one:

  • actually changed 'span' tag to 'a' tag.
  • I could use a['href'] = 'http://example.com'; to add the link to the tags, or doms, and leave it in HTML code.
Dinir
  • 171
  • 2
  • 10
0

Hello Put these code in your head section

<script type="text/javascript">
      function openWindows(argument) {
        window.open('http://example.com')
      }
  </script>

and replace your span tag with the following code...

<span class="link img" onClick="openWindows();">text</span>

May check this code and it work, I hope this will also help you.

Bharat Makvana
  • 209
  • 1
  • 5