Javascript outerHTML - Shows text on new blank page - Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maize
    New Member
    • Nov 2008
    • 3

    Javascript outerHTML - Shows text on new blank page - Problem

    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
    Code:
    <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.
    Code:
    <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.
    Code:
    <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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.
    Code:
    <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.

    Comment

    • Maize
      New Member
      • Nov 2008
      • 3

      #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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.

        Comment

        • Maize
          New Member
          • Nov 2008
          • 3

          #5
          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

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You're welcome. Glad to help :)

            Comment

            Working...