mahen23
mahen23

Reputation: 737

Browser says function not referenced

I have been knocking my head over this. I do not want to go through PHP functions to create cookies. The javascript functions are defined just above inside a Jquery document ready. JQuery libraries are loaded just about all those javascript functions.

<script type="text/javascript">
  $( document ).ready(function() {

    function superadmin()
    {
        document.cookie = "user=superadmin";
        window.location = "list.php";
    }

    function user()
    {
        document.cookie = "user=user";
        window.location = "list.php";
    }       
  });
</script>
<script type='text/javascript'>
  $( document ).ready(function() { 
    superadmin(); 
  });
</script>    

<div class="container">

Upvotes: 0

Views: 155

Answers (4)

Drago96
Drago96

Reputation: 1407

this is a problem related to javascript's scope. A jquery document.ready event use a seperate scope, so if you create a variable there, it can't be accessed from other places.

A solution to this is to create a namespace, which in this situation is an object where you attach the variable. The above problem could be solved this way:

<script type="text/javascript">

var Namespace = {};

$( document ).ready(function() {

  Namespace.superadmin = function()
  {
    document.cookie = "user=superadmin";
    window.location = "list.php";
  }

  Namespace.user = function()
  {
    document.cookie = "user=user";
    window.location = "list.php";
  }       
});
</script>
<script type='text/javascript'>
$( document ).ready(function() { 
  Namespace.superadmin(); 
});
</script>    

<div class="container">

EDIT:

For the specific situation above, the solution posted by Quentin is better. This is an acceptable solution assuming that you can't avoid having two seperate listeners (maybe in different files). You can of course also use a scope that is accessible from both scopes, which in this situation is the window scope.

Upvotes: 0

slou
slou

Reputation: 34

Your code is not in the same scope, try this:

superadmin = function()

instead of

function superadmin()

see http://www.w3schools.com/js/js_scope.asp for more details

Upvotes: 0

Quentin
Quentin

Reputation: 943686

Function declarations are scoped to the function they are declared in.

superadmin is defined in an anonymous function that is passed to ready() so can't be called outside of that anonymous function.

First, delete this:

<script type='text/javascript'>
  $( document ).ready(function() { 
    superadmin(); 
  });
</script>    

Second, put the call to superadmin(); inside the existing ready event handler function.

Upvotes: 3

Rajesh Patel
Rajesh Patel

Reputation: 2036

I have done cookes in javascript by this way

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}

function deleteCookie(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

 setCookie("userid",14);
 setCookie("username","rajesh");

var userid = getCookie("userid");
var username = getCookie("username");

Upvotes: -1

Related Questions