0

I am trying to hide the result of a C# method which is called from my javascript code in my aspx file. When I view the page source I want to return value, presently 'HI_MOM!', to not be visible.

My ASPX:

<html>
  <head>
    <title>Hide Me</title>
  </head>
  <body><div id="center"><div id="fig">
    <script type="text/javascript">
        var url = <%="'"+magic()+"'"%>;
        document.write(url);
    </script>
  </div></div></body>
</html>

My C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TestTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string magic()
    {
        return "HI_MOM!";
    }
}

Source Code after Running:

<html>
  <head>
    <title>Hide Me</title>
  </head>
  <body><div id="center"><div id="fig">
    <script type="text/javascript">
        var url = HI_MOM!;
        document.write(url.toString);
    </script>
  </div></div></body>
</html>

Basically, I want to make it so that when I view the source code for the page the line "var url = HI_MOM!;" is not visible to the user or is masked in some way.

EDIT:

ANSWER: (with thanks to @Shadow Wizard for pointing me in the right direction)

In TestTest.aspx:

<script type="text/javascript" src="./jquery.js"></script>

. . .

 jQuery.ajax({
    type: 'POST',
    url: 'TestTest.aspx/magic',
    cache: false,
    data: '{}',
    contentType: 'application/json; charset=utf-8',
    dataType: 'text',
    success: function (msg) {
        var result = eval('(' + msg + ')');
        result = eval('('+result["d"]+')');

        //do something with your string =]
    }
 });

In TestTest.aspx.cs:

[WebMethod]
    public static string magic()
    {
        return "HI_MOM!";
    }
Joshua Abrams
  • 377
  • 1
  • 5
  • 17
  • Can't you just delete the line from your aspx? –  Sep 12 '11 at 11:50
  • If it's not visible, then how will you use it? What's the purpose of this? – wasimbhalli Sep 12 '11 at 11:52
  • That would give you a JavaScript error because `HI_MOM` is not defined(it's not a defined variable and even no `string`). You want to return `'HI_MOM!'` from your CS-method. But apart from that, if you don't want the user to see it, you should not deliver it as javascript at all. – Tim Schmelter Sep 12 '11 at 11:52
  • @Inuyasha If I did that then I would not receive the data I need.... – Joshua Abrams Sep 12 '11 at 11:53

3 Answers3

0
var url = <%= "'" + magic() + "'"%>;
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
0

Everything you want to run as javascript will be viewable to the user. You could make it look less obvious what it is doing or even go as far as encrypting it, but it's still all there in the browser.

The only way to truly hide this would be to do it all server side.

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

You will have to use AJAX for this.

Using jQuery is one option, with this you can turn magic to be Page Method then consume it.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • This is definitely what I was looking for. I am very new to ASP.NET/javascript and I know nothing about AJAX or jQuery. Would you be able to find me a small full example with this, because I am having difficulty understanding how to implement something like this. – Joshua Abrams Sep 12 '11 at 12:03
  • [This](http://stackoverflow.com/questions/1176603/using-jquerys-getjson-method-with-an-asp-net-web-form/1190225#1190225) answer has full code - C#, HTML and JavaScript. You really don't need anything more than that. If you don't understand some part there, feel free to ask. :) – Shadow The GPT Wizard Sep 12 '11 at 12:51