Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:50:42+00:00 2026-06-05T23:50:42+00:00

I am trying to run some javascript code that is created by my codebehind

  • 0

I am trying to run some javascript code that is created by my codebehind document.

<script type="text/javascript">

    function GetMap() {
        //some javascript
    }

    function AssetDescription(var1, var2, var3) {
        //some javascript using var1, var2, and var3
    }

    function test() {
        alert("test position 1");
        var onloadText = document.getElementById("points").value;
        alert(onloadText);
        eval(onloadText);
        alert("after eval");
    }
    window.onload = test;
</script>

<body runat="server">
<input type="hidden" id="points" runat="server" />
</body>

CodeBehind

protected void Page_Load(object sender, EventArgs e) {
    points.Attributes.Add("value", Iterations());
}

public string Iterations() {
    string toReturn = "GetMap(); \n";
    //assetRow is an array of strings. The strings are in the format "var1, var2, var3"
    for (i=0; i<numberOfAssets; i++)
    {
        toReturn = toReturn + "AssetDescription("+assetRow[i]+"); \n";
    }
    return toReturn;
}

Hopefully I have made all of this as clear as possible. This is all the code I believe is relevant to my question. I am open to other ways of setting this up.

I have tried setting the body.onload element to include my javascript from the codebehind directly, but to no avail. I would like to add this code directly in my tags, but I don’t think ASPX has that capability, so this is the workaround I have created.

This seems to be the most promising method i have found. I have searched these forums, and most of you say that eval() is a bad idea. Needless to say, it’s not even working. But I would love a more robust alternative if any.

The alert("after eval") alert never fires, but alert("test position 1") and alert(onloadText) both fire.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:50:45+00:00Added an answer on June 5, 2026 at 11:50 pm

    instead of setting it to and html element and using eval to run it, you can created a public variable, and print the public variable on to the page. This will mean the string contents of the variable is going to be printed, which will be the JavaScript to be run.

    So front end

    <script type="text/javascript">
    
        function GetMap() {
            //some javascript
        }
    
        function AssetDescription(var1, var2, var3) {
            //some javascript using var1, var2, and var3
        }
    
        function test() {
            alert("test position 1");
            <%= generatedScript %>
            alert("after eval");
        }
        window.onload = test;
    </script>
    
    <body runat="server">
    <input type="text" id="points" runat="server" />
    </body>
    

    Code Behind

    public string generatedScript = string.empty;
    
    public string Iterations() {
        string generatedScript = "GetMap(); \n";
        //assetRow is an array of strings. The strings are in the format "var1, var2, var3"
        for (i=0; i<numberOfAssets; i++)
        {
            generatedScript = generatedScript + "AssetDescription("+assetRow[i]+"); \n";
        }
    }
    

    Should be fine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I'm trying to do is run some JavaScript code that will enter text
I am trying to run some acceptance tests for javascript code. However, when I
I'm trying to run some basic javascript code with JSF 2. I'm using the
I have some code in umbraco that is run and creates a javascript file
I'm trying to load in my WebView some HTML code that contains JavaScript. Now
I'm trying to run some conditional javascript to position a div such that, if
I'm trying to run some NUnit tests that use NMock2 for mocking. These tests
Ultimately I am trying to run some code which depends on the value (or
I am trying to run some Scala code on my OSX machine and keep
I'm a student, and I'm trying to write and run some test code for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.