3

I've got a small script that detects if the user is using a certain browser

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(is_chrome){
    $('.ifChrome').attr('style', 'display:block;');
    $('.ifChrome').html($('noscript > div').html());
};

If they are using this browser, we want to display a div tag and show the HTML from a different div tag inside.

<noscript>
    <div class="note">
        Your browser does not properly support the WMD Markdown Editor.<br />
        Please use <a href="/about/markdown" target="_blank">Markdown<.a> to style your input.
    </div>
</noscript>
<div class="hidden ifChrome note"></div>

What I'm trying to do is show the "not supported" text from inside my <noscript> tag to users who are using this browser (because WMD Markdown doesn't work properly with it).

My existing Javascript is not working. Any help would be greatly appreciated.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376

3 Answers3

10

This is a bit late but you can do:

var noscriptContents = $($('noscript').html());
$('.ifChrome').append(noscriptContents);
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
5

You cannot access the content of a noscript-element using javascript in chrome. So you'll have to find a different approach that doesn't use a noscript-element. Read more about that problem here

Community
  • 1
  • 1
Simon
  • 3,509
  • 18
  • 21
0
<noscript id="noscript-main">
      <div id="div-inside-noscript">
        <span>Your content here</span>
      </div>
      <div>1</div>
      <div class="no-action">2</div>
 </noscript>
 <div id="show"></div>
<script>
      var noscript_main  = $(`#noscript-main`);
      var noscript_main_text = noscript_main.text();
      $.each($(noscript_main_text),function(k,v){
        var obj = $(v);    
        // if you want get the id of element:    
        if( obj.get(0).tagName == `DIV` ) {
          console.log( obj.prop(`id`));
        }
        //if you want display div-inside-noscript inside div#show
        if(obj.prop(`id`) == `div-inside-noscript` ){
          var obj_html = obj.html();  
          $(`#show`).html(obj_html);
        }        
      });
      //if you want remove noscript tag
      noscript_main.remove();      
 </script>
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '23 at 11:34