How to run jquery script if html page is the home page...?

23,805

Solution 1

I'm posting another answer in case you can't implement the Master Page solution.

You could use a flag element to tell jQuery it's the homepage, because the URL solutions posted earlier can easily break.

Somewhere in your Homepage content, simply place this.

<span id="homepage-flag" style="display: none" />

And then using jQuery, check if the element exists and run your code. It's a pretty poor solution but it will work if you can't get my other answer to work.

if($("#homepage-flag").length > 0) {
    // run code for homepage
}

Solution 2

How about a script Content Place holder that's inside the <head> of the MasterPage, and then placing content inside the placeholder from your homepage.

Basically..

In your Master Page

<head>
<title>hello</title> etc...
// add jQuery here

<asp:ContentPlaceHolder ID="jQueryCode" runat="server"></asp:ContentPlaceHolder>

And then in your Home Page

<asp:Content ContentPlaceHolderId="jQueryCode" runat="server">
    // run jQuery script here
</asp:Content>

Also - if you're not using jQuery on the other pages, you can remove it from the MasterPage and add it right above your script inside the home page <asp:Content />

Share:
23,805

Related videos on Youtube

chrisb
Author by

chrisb

Updated on January 18, 2020

Comments

  • chrisb
    chrisb over 4 years

    I'm working with an external team with our website and they recently added one of my scripts to the .NET MasterPage of the site... well it did finally get my script running but now... it loads Banners on 'every' page on the site.

    How can I write an 'if' statement that basically says... if this is the home page... run this script... if not don't...?

    • Quentin
      Quentin over 13 years
      Isn't the point of a master page to include the data in every page? This sounds horribly like "Doctor! Doctor! It hurts when I fire the gun at my foot!"
    • chrisb
      chrisb over 13 years
      I'm not sure how helpful that was, but it was funny...!
  • Jim L
    Jim L over 13 years
    +1 Most any other way of doing this would smell of the equivalent of requiring a parent class to know about its sub classes.
  • Marko
    Marko over 13 years
    @chrisb I feel bad now for providing this (ugly) alternative - why can't you do use the code above? It's a much better solution.
  • Marko
    Marko over 13 years
    lol. I can't say I've ever posted 2 answers, one of which got 5 upvotes and the other got accepted.
  • chrisb
    chrisb over 13 years
    I'm just the frontend java guy... and don't have access to the Master Pages... I just need a 'dirt' fix until those guys get back in tomorrow... ;-)
  • chrisb
    chrisb over 13 years
    I am in dialog with the backend devs on implementing this solution.
  • Sleek Geek
    Sleek Geek about 9 years
    This answer saved my life.
  • Johan Nordli
    Johan Nordli about 9 years
    One tip, use "js-homepage-flag", then you know that this element will be used in your javascript and in your javascript only.
  • Dan Beaulieu
    Dan Beaulieu almost 9 years
    gross... I cant wait to use this! +1 :)