0

I have created Master Page EXAMPLE1.Master for my .net web application. Their I am storing value in JavaScript variable. I want to retrieve that variable in another JS File.

EXAMPLE1.Master:-

<script language="javascript" type="text/javascript">
        $(document).ready(function() {
            var pageAccess = '<%=Session["UserAccess"]%>';
            masterPageLoad();
        });
</script>

<script language="javascript" type="text/javascript" src="JS/pageActions.js">
</script>

pageAction.js

//Retrieve pageAccess variable here.

Definition of masterPageLoad(); is present in pageAction.js file

2
  • 1
    Can't you pass a parameter to masterPageLoad() ? That's what parameters are designed for !! Commented Sep 6, 2012 at 14:10
  • i dont want value in just masterPageLoad() i want it in all functions in pageAction.js Commented Sep 6, 2012 at 14:12

3 Answers 3

1

declare your pageAccess variable, before $(document).ready(function() {

like

var pageAccess = '<%=Session["UserAccess"]%>';
$(document).ready(function() {
   masterPageLoad();
});
Sign up to request clarification or add additional context in comments.

Comments

1

Move your variable declaration outside the function

var pageAccess = '<%=Session["UserAccess"]%>';
$(document).ready(function() {
        masterPageLoad();
});

This variable should now be visible in any JS file.

Comments

0

It would be preferable if you could do something like this:

$(document).ready(function() {
    masterPageLoad('<%=Session["UserAccess"]%>');
});

And then update your pageActions.js accordingly:

function masterPageLoad(pageAccess) {
    ...
}

But if you need to work with an external variable, the reason it's currently not working is that it's defined within the scope of the DOMReady handler. You should either extract the variable declaration to be outside of the DOMReady handler, or you should create a global variable:

window.pageAccess = '<%=Session["UserAccess"]%>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.