www.webdeveloper.com
Results 1 to 7 of 7

Thread: Need help to find element tag combination and delete certain parts

  1. #1
    Join Date
    Oct 2013
    Posts
    5

    Need help to find element tag combination and delete certain parts

    1.) Assume I have a (HTML) page which contains none, one or more occurencies of

    ...
    <p>
    ....
    <br>
    <br>
    </p>
    ...


    If a <br> tags follows another, second <br> element with only whitespaces in between
    then BOTH <br> tags should be deleted.

    How can I perform this replacement/deletion with javascript?

    2.) Assume I have a (HTML) page which contains none, one or more occurencies of

    ....
    <h2>....</h2>
    <br>
    ...

    Similar to 1.) when the closing tag </h2> is followed by a <br> (only whitespaces are allowed between) element, then the <br> tag should be deleted.

    How can I perform this replacement/deletion with javascript?

    Thank you
    Peter

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    To be clear, JavaScript can not remove those elements from HTML code directly only what exists in the DOM tree on load. This means if the HTML code that the web browser requests has those elements in the page, having javascript remove them on every page load is pointless, simpler to edit the HTML document in the first instance.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by \\.\ View Post
    To be clear, JavaScript can not remove those elements from HTML code directly only what exists in the DOM tree on load. This means if the HTML code that the web browser requests has those elements in the page, having javascript remove them on every page load is pointless, simpler to edit the HTML document in the first instance.
    Yes, I know, this all.

    However I have no access to HTML source.
    So I need to modify the HTML in loaded browser by DOM Javascript.

    If its easier for you then think of a corresponding XML document.

    I just need the suitable JS cmds.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Sorry, that doesn't track, if you are running a script in a HTML page then you do have access to the HTML code.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Nov 2010
    Posts
    1,097
    Quote Originally Posted by \\.\ View Post
    Sorry, that doesn't track, if you are running a script in a HTML page then you do have access to the HTML code.
    well, not quite. one scenario would be that pstein might be loading a userscript, in which case the only way to modify the html would be through javascript.

    you could try something like this:
    Code:
    document.body.innerHTML=document.body.innerHTML.replace(/<br>\n<br>/g,"").replace(/<\/h2>\n<br>/g,"</h2>");

  6. #6
    Join Date
    Oct 2013
    Posts
    5
    Quote Originally Posted by xelawho View Post
    you could try something like this:
    Code:
    document.body.innerHTML=document.body.innerHTML.replace(/<br>\n<br>/g,"").replace(/<\/h2>\n<br>/g,"</h2>");
    Thank you, but two more questions:

    1.) When I look at

    replace(/<\/h2>\n<br>/g,"</h2>");

    Shouldn't the forward slash be marked too like in:

    replace(/<\/h2>\n<br>/g,"<\/h2>");

    ?

    2.) Looking at

    replace(/<br>\n<br>/g,"")

    I guess \n represents only ONE page break. What about other white spaces like blanks+tabs?
    What about if more than one page break occurs? Wouldn't

    replace(/<br>\s*<br>/g,"")

    better? Or is

    replace(/<br>\\s<br>/g,"")

    needed?


    @

    \\.\





    @ \\.\ : Have a look at great Greasemonkey plugin tool for Firefox.
    Wit hthat tool you can apply CSS and javascript to webpages AFTER loading!

  7. #7
    Join Date
    Nov 2010
    Posts
    1,097
    1. no - because /<\/h2>\n<br>/g is a regular expression whereas "</h2>" is a string

    2. yes, you will have to tweak it to suit your specific needs

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles