Quote:
Originally Posted by jonquinn
I am trying to create a conditional comment scenario that is based on a web address. I am working with an ecommerce site that I want to put a nav menu promotion on all the pages except the home page. The system we are using only allows a post promotion to ALL or one specified URL.
I have tried everything I can think of to create a URL condition based comment but nothing has worked.
|
Code:
<script type="text/javascript">
<!-- The deprecated media type text/javascript is used over the media types application/javascript and application/ecmascript for Internet Explorer compatibility. -->
var d = document;
if (/^http:\/\/www\.sitename\.com\/(\?.{3,})?(#.+)?$/.test(d.URL)) {
// do something
}
else {
// do something
}
</script>
The above code accounts for query strings (e.g., http://www.sitename.com/?parameter=value) and fragment identifiers (e.g., http://www.sitename.com/#fragment).
It doesn’t account for the omission of “www.” (e.g., http://sitename.com/), which your server may or may not allow; pages that might use the HTTPS scheme (e.g., https://www.sitename.com/); port numbers (e.g., http://www.sitename.com:
port/; or pages that misuse fragment identifiers (e.g., http://www.sitename.com/# from
href="#" or elsewhere).
Quote:
Originally Posted by jonquinn
I tried modifying the code below to change the if output to a variable comment tag and then write the variable further in the script with a document.write.
|
document.write is poor practice. Use DOM Core methods instead.
Quote:
Originally Posted by jonquinn
<SCRIPT LANGUAGE="JavaScript">
<!--
url=location.href
if (url=="http://www.sitename.com") {alert("Welcome To sitename")}
else {alert("welcome")};
// -->
|
Don’t put SGML comment tags around your scripts (
<!-- and
-->) or use the deprecated
language attribute. For the latter, use the
type attribute instead. If you’re just looking for the current URL, then the DOM2 HTML
document.URL is a better choice than the non‐standard
location.href.