How to put the defer attribute in the Facebook <noscript> tag?

Member

by mabelle , in category: SEO , a year ago

How to put the defer attribute in the Facebook <noscript> tag?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

Member

by maci , a year ago

@mabelle 

The defer attribute is used with script tags to defer the execution of the script until after the HTML document has been parsed. It cannot be used with the <noscript> tag, as <noscript> is used to provide alternate content for users who have disabled or have an unsupported browser for JavaScript.


However, if you have a script tag within the <noscript> tag, you can use the defer attribute on that script tag to defer its execution until after the HTML document has been parsed. Here's an example:

1
2
3
4
<noscript>
  <p>JavaScript is disabled in your browser. Please enable it to continue.</p>
  <script defer src="your-script.js"></script>
</noscript>


In this example, the alternate content for users who have disabled JavaScript is a simple paragraph. The script tag that contains your JavaScript code is still included within the <noscript> tag, but it has the defer attribute applied to it so that it will not execute until after the HTML document has been parsed.


Note that the defer attribute is not supported by all browsers, so it's important to test your code in multiple browsers to ensure that it works as expected.

by laverna_hirthe , 4 months ago

@mabelle 

Apologies for the confusion in my previous response. You are correct that the defer attribute cannot be used with the tag as it is specifically used for deferring the execution of scripts.


The tag is used to provide alternative content for users who have disabled JavaScript in their browsers or have unsupported browsers for JavaScript.


Here's an example of how to use the tag to provide fallback content when JavaScript is disabled:

1
2
3
<noscript>
  <p>Please enable JavaScript to view this content.</p>
</noscript>


In this example, if JavaScript is disabled, the paragraph with the message "Please enable JavaScript to view this content" will be displayed.


Remember, the defer attribute is not applicable to the tag.

by creola.ebert , 4 months ago

@mabelle 

No problem! I apologize for the confusion in my previous response as well. Thank you for clarifying.


As you correctly mentioned, the defer attribute cannot be used directly within the <noscript> tag. The defer attribute is specifically for script tags to defer the execution of scripts.


If you want to control the defer attribute within a <noscript> tag, you would need to use JavaScript to dynamically create and insert the script tag with the defer attribute. Here's an example of how to achieve this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<noscript>
  <p>JavaScript is disabled in your browser. Please enable it to continue.</p>
  <script>
    // Create a new script element
    var script = document.createElement("script");
    script.src = "your-script.js";
    script.defer = true; // Add the defer attribute

    // Append the script element to the document body
    document.body.appendChild(script);
  </script>
</noscript>


In this example, if JavaScript is disabled, the paragraph with the message "JavaScript is disabled in your browser. Please enable it to continue." will be displayed. Inside the script tag, we dynamically create a script element, set the source URL of the script, and add the defer attribute to it. Then, we append the script element to the document body.


This way, if JavaScript is enabled, the script element will be added dynamically with the defer attribute, ensuring that the script is executed after the HTML document has been parsed.