SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    First Load [Call function]

    is there any function that can tell if the page is being loaded for the first time? I need to call a function only when the page is initially loaded.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,196
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could use localStorage to determine whether a user has visited or not but it only works in modern browsers and IE8+, for a fallback if you need to support IE7 and below cookies would be the next option.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    You could use localStorage to determine whether a user has visited or not but it only works in modern browsers and IE8+, for a fallback if you need to support IE7 and below cookies would be the next option.
    ok i don't need the function to call the first time they ever visited the site, but each time they visit the site. something like a
    Code:
    window.onload

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you place your JavaScript at the bottom of the page where it belongs then it can run straight away without needing window.onload
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    If you place your JavaScript at the bottom of the page where it belongs then it can run straight away without needing window.onload
    ok understood but how would I know if the function is being called for the first time. Ok the senario is that the first function my page loads is function1. However the first time function1 is called it needs to do something like replaceState. However the next time I call function1 while on this site it checks to see if it was called before if not then no replaceState. I hope this better explains what I am trying to do.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,861
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Code:
    function1 = function() {
       // code to run only the first time function1 is called goes here
       function1 = function() {
          // code to run every time function1 is called goes here
       }
       function1(); // leave out this line if the code to run every time should not run the first time
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    function1 = function() {
       // code to run only the first time function1 is called goes here
       function1 = function() {
          // code to run every time function1 is called goes here
       }
       function1(); // leave out this line if the code to run every time should not run the first time
    }
    Ok never saw this before will give it a go.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •