开发者

How to access javascript variable in .net master page

I need to enable a script which is present in a .net master page, in only selected html pages that use this master page开发者_运维技巧. can this be achieved by declaring a variable in javascript of html pages where I need this script and set the variable to some value, so that I can enable the scripts in master page only when variable is NotNullorEmpty? Does some one know if this works. If so how to get javascript variable in html, in to .net master page ?


There are 2 ways I can think of.

The first if to assign the variable to a hidden input field. Which can be accessed in the Request.Form.

html

<input type="hidden" value="your value" name="Hidden1" id="Hidden1"/>

javascript

document.getElementById('Hidden1').value = "an other value";

C#

var myValue = Request.Form["Hidden1"];

The second is saving the variable into a cookie which can be accessed in Request.Cookies.

var myValue = Response.Cookies["Cookie1"].Value;


The most flexible form of this that I have seen is to render a JSON object directly to the page as script (adding any other script you might need also), then making the javascript code fill in a hidden field with JSON data on form submit for the server to parse.

Here are the basic steps:

  • Create an empty hidden field for the server to read when the client submits, but send it to the client empty.
  • Create a string which is the script and JSON data you need to send to the client. (Use the JavaScriptSerializer .NET class.)
  • Fill a LiteralControl with the script tag and its contents. (This works well in the OnPreRender step, but you can do it elsewhere.)
  • Include JSON2.js (it's all over the web - get it from a trusted source)
  • Write javascript code to fill the hidden field on submit, using JSON2.stringify() to push any javascript object into that field.
  • On the server, you can again use JavaScriptSerializer to read the values from the submitted object.


What you are basically wanting to do is test for javascript.

There are a few ways to do this, but I would use both a server-side and client-side cookie.

In your global.asax's onbeginrequest, test for a server-side cookie of "js-test-s"

  • Cookie Exists, check for "js-test-c"
    • "js-test-c" Cookie Exists, JS enabled
    • "js-test-c" No Cookie, JS disabled
  • Cookie doesn't exist...
    1. Set the "js-test-s" cookie
    2. output a simple html document...
      [html]
        [head]
          [meta refresh tag set to 1 second /]
          [script to set "js-test-c" cookie, then force refresh /]
        [/head]
        [body][/body]
      [html]
    3. Browser will refresh the request with one or both cookies set.

WARNING: You may want to store the original request details, and redirect to another location in case cookies are disabled.


I might be not understanding the question correctly but as far as I can tell the easiest way to do this is by creating a property on the master page and then setting it on all pages that need to execute the script:

In Master page

public bool EnableScript
{
    set
    {
        if (value == true)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "alert('test');", true);
        }
    }
}

In all pages that need to run javascript that use the current master page:

protected void Page_Load(object sender, EventArgs e)
{
    this.Master.EnableScript = true;
}

If you have to output your JavaScript on every page, even on the once that don't execute it (why?) then just add it as a function and then inside your property add a code to call that function, and set your property to true on all pages that require JavaScript to execute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜