Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This question has already been answered for registration,
participation is to improve functionality and knowledge. Thanks.

<script language="JavaScript">
<!--
function urlCheck() {
if(
window.location.href=="address to check"
)
{
/* some code to load */
}else{
document.write('<div>testing my super loaded div code sample</div>')
}
}
urlCheck() /* autoLoadFunction */
//-->
</script>

now place the script where you want the content to be checked and loaded in your design.

Code Description

urlCheck,
is the name given to the function.
you can change it, if you do, change all urlCheck references within the code.

window.location.href,
this code returns the current address as http://...
use to identify certain pages you want the content to be loaded.
make as many if..else combinations as necessary.

document.write,
build the content to be loaded first,
and type with later in one unique line,
or create multiple document.write for each line of your content.

share|improve this question
5  
I'd probably fix the indentation. –  Florian Margaine Jun 24 '13 at 7:58
 
Add // at the beginning of the line with <!-- or remove it from the line //--> –  simpleBob Jun 24 '13 at 8:11
3  
What is your question? What is the first paragraph supposed to mean? –  svick Jun 24 '13 at 8:31
 
Basic English lessons? Flaming aside, you do realize that we're here to do code review, and we do not accept anything that can't be answered? Because it seems like you want to use us as a blog, but we're not your personal blog. ... So, do you now have code which we can review? –  Bobby Jun 24 '13 at 13:55
add comment

1 Answer

<script language="JavaScript">

The script tag does not have a property language anymore, that property is deprecated. You should use the property type instead. Also the correct value for that property would be text/javascript.


<!--

This is a relict from somewhere before all browsers could do JavaScript, please don't do it anymore.


function urlCheck() {

Nothing to say here. Except you should think about making it a habit to activate strict mode by default.


if(
window.location.href=="address to check"
)
{
/* some code to load */
}else{
document.write('<div>testing my super loaded div code sample</div>')
}
}
  • Your indentation and use of whitespace is completely broken, fix it.
  • I've never seen a single if which was split over three lines, fix it.
  • You should not use == unless you know what it is doing. == performs casting as necessary to compare two values, while === compares their type and value.
  • I'm not a fan of creating HTML-elements in a string (document.createElement()), but for sure the opinions differ on this one vividly.

urlCheck() /* autoLoadFunction */

I suggest you either write your code directly and don't wrap it in a function (because that does nothing if you're going to directly run it after declaring) like this:

<script type="text/javascript">
    if(window.location.href === "address to check") {
        // some code to load
    } else {
        document.write('<div>testing my super loaded div code sample</div>')
    }
</script>

Or run your code when the document is ready, by putting it at the end of the page (or use a framework which supports something like $(document).ready()):

<script type="text/javascript">
    urlCheck();
</script>

//-->

As I already said, that's a relict, stop doing it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.