1

Is it correct and valid to use something like this:

<noscript>
<?php require_once 'somefile.php'; ?>
</noscript>

I've been searching for this answer a lot but unfortunately not much luck. Here is a quote from one of the answers by @SLaks on this page.

"If you put PHP code inside a tag, it will always execute, whether Javascript is enabled or not. However, the code's output will only be visible if Javascript is disabled."

However I am not very clear if it is valid to use php within a <noscript> tag.

3
  • You can use PHP in any tag - PHP runs at server side. Commented Jul 19, 2013 at 7:31
  • Yes it is valid. But like what you quoted. The php content will not show if Javascript is disabled. Commented Jul 19, 2013 at 7:33
  • <noscript> is a very binary approach to dealing with the possibility of JavaScript failing, generally speaking, you should avoid it and follow the principles of Progressive Enhancement and Unobtrusive JavaScript instead. Commented Jul 19, 2013 at 7:37

4 Answers 4

2

PHP operates on a stream of text. It doesn't care what the text is. It is either just outputting the input or (when inside a <?php section) running PHP. It doesn't know about HTML.

Validity (in this context) only matters in the context of the generated HTML. The HTML will be valid if the PHP outputs valid content, and that will depend on what require_once 'somefile.php'; outputs.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are correct. In that somefile.php I am actually displaying a simple 'Javascript Disablled' message to the user along with an option for him to enable it if he wants to or else he can clink on a link which will take him to a non-javascript version of the site. Something similar to what Google does it on it's Gmail site. I am styling this page with some css for neatness hence wanted to take the code to a separate file so that it does not make my code untidy.
2

Of course it is. <noscript> is evaluated client side, after PHP preprocessing. So whatever you put in your somefile.php, it will be processed, and then the result sent to the client inside the <noscript> tag. However, as said by Slaks, the generated output won't be displayed until JS is disabled.

The way you include PHP in your pages has nothing to do with HTML validation, as long as the generated page is valid HTML.

Comments

1

Oh course you can. Noscript is designed to be used when user has disabled JavaScript, so why not? But be aware that it will always executed while rendering page. Almost all users on the Internet use JS, so it can be a little overhead.

Comments

1

It is valid to use PHP in the tag - PHP is evaluated on the server, and not send to the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.