By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Javascript outerHTML - Shows text on new blank page - Problem

P: 3
Hi Guys,

i'm having a problem with a little Javascript in a link


Sorry that it is all in a line but it is actually a link. Please wrap.

This results in a new page with only "Initially processing" showing
Expand|Select|Wrap|Line Numbers
  1. <a href="javascript:var Aobj = document.getElementsByTagName('span'); for (var i = 0;i &lt; Aobj.length;i++) { if (Aobj[i].className === 'SLA') { Aobj[i].innerHTML += 'Initially processing'; Aobj[i + 1].innerHTML = ''; } }"><br/>Read more</a>
This (alert added) results in a correct output. But i don't want an alert.
Expand|Select|Wrap|Line Numbers
  1. <a href="javascript:var Aobj = document.getElementsByTagName('span'); for (var i = 0;i &lt; Aobj.length;i++) { if (Aobj[i].className === 'SLA') { Aobj[i].innerHTML += 'Initially processing'; Aobj[i + 1].innerHTML = ''; alert("stop") } }"><br/>Read more</a>
This (whateverbla added) results in an error but executes correctly.
Expand|Select|Wrap|Line Numbers
  1. <a href="javascript:var Aobj = document.getElementsByTagName('span'); for (var i = 0;i &lt; Aobj.length;i++) { if (Aobj[i].className === 'SLA') { Aobj[i].innerHTML += 'Initially processing'; Aobj[i + 1].innerHTML = ''; whateverbla } }"><br/>Read more</a>

Can you tell me if i am doing anything wrong or tell me what i could do to stop executing (break results in empty page only).
I can not exclude that the error is somewhere else because i am working in a company corporate portal environment and i don't control everything there.


Thanks in advance and
best Regards

Marcel
Nov 3 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
It's caused by the href attribute. You need to cancel it if you want to prevent a new page being loaded. What you can do is use onclick instead, e.g.
Expand|Select|Wrap|Line Numbers
  1. <a href="#" onclick="var Aobj = document.getElementsByTagName('span'); for (var i = 0;i < Aobj.length;i++) { if (Aobj[i].className === 'SLA') { Aobj[i].innerHTML += 'Initially processing'; Aobj[i + 1].innerHTML = ''; } }; return false;"><br/>Read more</a>
or declare a function with this code and call that instead.
Nov 3 '08 #2

P: 3
Thanks for the answer. Exactly what i was looking for.

But the editor in the corporate portal removes any javascript and also removes the "onclick" tag in the link before publishing my html. (not in the href part for some reason). This is due to some security issues.

If you know any workarounds let me know. If not, its ok. The answer already helped me a lot.

Thanks and Regards
Marcel
Nov 4 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Yes, there is. Add void(0) or void 0 at the end in the href. That should solve your problem. Of course, I would recommend using the onclick as best practice and you should avoid the "javascript:" protocol in the href attribute, but I guess your hands are tied.
Nov 4 '08 #4

P: 3
Hi acoder,

that totally did it. Thanks a lot, i'll try not to use it if it is not
necessary but in this case, yes, my hands are tied and i am
happy to have found this solution with your help.

Thanks and Regards
Maize
Nov 4 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad to help :)
Nov 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.