Question Overflow
Question Overflow

Reputation: 11255

How to remove noscript tag wrapper with jquery?

I want to remove noscript tag that is wrapping round an image:

<a href="some_image.jpg">
 <noscript>
  <img src="some_image.jpg">
 </noscript>
</a>

I tried unwrap(), but it doesn't work inside noscript, next I tried the html() method:

$('a').html(function(index, oldhtml){
   return oldhtml.replace(/\<noscript\\?>/g, '');
});

Though the tag is removed, it produces a string instead of DOM:

<a href="some_image.jpg">
 "
  <img src="some_image.jpg">
 "
</a>

How to remove the noscript tag wrapper while keeping the img element untouched?

Upvotes: 1

Views: 5781

Answers (6)

Afzal K.
Afzal K.

Reputation: 913

Why do we even need jQuery?

document.querySelectorAll("[unwrap]").forEach(function(a){a=a.textContent;a=document.createRange().createContextualFragment(a);document.body.appendChild(a)});
<a href="some_image.jpg">
 <noscript unwrap>
  <img src="some_image.jpg" alt="some_image">
  <h1>Hello World</h1>
 </noscript>
</a>

Hope this helps!

Upvotes: 0

user5781320
user5781320

Reputation:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD>
<TITLE>noscript can change the Internet forever</TITLE>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
$(document).ready(function(){
    $('noscript').replaceWith(function() {
        return this.textContent || this.innerText;
    });
    $("p#javascripton").css("background-color", "yellow"); 
    $("p").click(function(){
        $(this).hide();
    });
}); 
//-->
</SCRIPT>
<noscript>
<p>
Noscript's usage today can be logical for <a href="google.com"><p id="javascripton">eg pc/laptop/high quality tablets usage the complete website with all features:images high resolution,javascript<br><h1>OR without javascript so no high resolutions images inserted with a jquery automated script generated from some php+javascript scripts so have usage for 80% mobile application cause almost are from China ,so low quality products=low cpu,low ram :IN THIS CASE SOMEONE CAN THINK TO SWITCH HIS PHONE TO NO JAVASCRIPT USAGE SO IF ANY PROGRAMMER CAN ADAPT AN ENTIRELY APPLICATION TO THE METHOD I USED IN THIS EXAMPLE AUTOMATED HIS BROWSER IS ADAPT FOR ANY RANDOM ACTION ABOUT THE USER CHOISE(YOU UNDERSTAND "TO USE OR NOT JAVASCRIPT") SO HIS CHINESE PHONE CAN BE APROXIMATELLY APROACH LIKE QUALITY OF SPEED EXECUTION THE OTHERS PC/LAPTOPS/TABLETS QUALITY PRODUCTS.<BR><BR>This stupid example is the best example how no script tag can change the quality of services on this planet ,boost the speed of the internet connection and stops unnecessary use of A LOT OF INTERNET TRAFFIC on slow devices..a simple tag can change the entirely dynamic of programmer's views so entirely Planet's beneficts</h1><p></a> <br>
run this code in two instances :<br>with browser javascript enable <br>and without(browser's javascript disable or eg a firefox plugin noscript states on/off)
</p>
</noscript>
</BODY></HTML>

Upvotes: 0

Greenhorn
Greenhorn

Reputation: 1700

You can try this way:

var image=$('noscript').text();
$("noscript").remove();
$("a").append(image);

Demo Fiddle

Upvotes: 1

Ram
Ram

Reputation: 144659

You can replace the element with it's contents:

$('noscript').replaceWith(function() {
    return this.textContent || this.innerText;
    // return $('<div/>').html(this.innerHTML).text();
});

http://jsfiddle.net/x9Eaw/

Upvotes: 6

Andreas
Andreas

Reputation: 21881

Why not leave the noscript as it is and append the image yourself?

$("noscript").each(function () {
    $('<img src="' + this.parentNode.href + '" />').appendTo(this.parentNode);
});

fiddle

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try something like - it is in fact a very crude hack

jQuery(function($){
    $('a:has(noscript)').html(function(){
        return $(this).find('noscript').text()
    })
})

Demo: Fiddle

Upvotes: 2

Related Questions