0

i want to call javascript function which is already defined in ASP HiddenValue in server side(.ie in .cs file).

I have defined like below

in .cs side :

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "javascript:return CallFun();");
        HF2.Value = " function() { alert('yes its working.... Woooww')};";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Write("Yea its working woooooowww...");
    }

in aspx:

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:HiddenField ID="HF2" runat="server" />

in js side:

function CallFun() {
    var hf2val = document.getElementById('HF2');
    //var hf2val = document.getElementById('HF2').value;        
    hf2val();
    //window[hf2val]();        
    return false;
}

I tried many times and searched on google also but i am not get the correct answer. but it will works if i write like below

in .cs

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "javascript:return yes();");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Yea its working woooooowww...");
    }

in js:

function yes() {

    var just = function() { alert('yes its working.... Woooww')};
    just();
    return false;
}

in aspx:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

But I don't want to show like above. My aim is to hide javascript functionality like calendar functions in View source, Firebug.

mclark1129
  • 7,532
  • 5
  • 48
  • 84
Balu
  • 39
  • 7
  • 2
    You just can't hide javascript anyway. It's executed client side, and therefore you need it client side... – Laurent S. May 03 '13 at 13:37
  • Thanks but i am getting the code from Hidden value. So why dont i call using that code? but we can call justcall() function that i have shown in above code. – Balu May 03 '13 at 13:41
  • From where I see it there's no point debugging a code that shouldn't be written in the first place. Just put your javascript where it belongs, and it's certainly not in a hidden field... – Laurent S. May 03 '13 at 13:44
  • `eval` might help you, but I really, really, really discourage its use. Why do you want to hide the javascript code? One way or another, it will be seen by the client. – Sashenka May 03 '13 at 13:51
  • i have also checked with eval but no use – Balu May 03 '13 at 13:53
  • @Bartdude :Please check the code now – Balu May 03 '13 at 13:55
  • @Balu you're still using a hidden field ? Why for godsake ? Javascript is intended to be in a js file, or eventually inline if you need it, certainly not in a hidden field. – Laurent S. May 03 '13 at 13:58

1 Answers1

2

You're not going to be able to work with values from a HiddenField within your Javascript code, because those values don't actually get rendered in the HTML that is sent to the client's browser. Technically, they are persisted in the ViewState that is sent back and forth between the client, but that's not really something that Javascript can make use of.

Javascript code executes on the client's machine, which means you are required to send all of the code (and the values needed to run that code) to the client. The simplest way to hide a value would be to put it in a hidden HTML element like so:

<span id="myHiddenValue" style='display: none'>MyValue</span>

but as you already know, this is easily visible by a user simply by viewing the source of the page. The most you can hope for is to obfuscate your Javascript code, which makes it basically unreadable to humans while still being functional. It's not foolproof, and could probably be reversed by anyone using a minimal amount of effort. In many cases, obfuscation would also offer a side benefit of compressing your code, making it much smaller and decreasing your page load times.

Take a look at this question for some good examples of how you could accomplish this.

Community
  • 1
  • 1
mclark1129
  • 7,532
  • 5
  • 48
  • 84