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 6744553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:52+00:00 2026-05-26T12:05:52+00:00

Often the Javascript on a webpage will need to be able to access variables

  • 0

Often the Javascript on a webpage will need to be able to access variables that are known on the server. For example, the user’s username. Suppose that we don’t want to make a JSON/XML request as that would increase the complexity unnecessarily and also the number of page hits. We want to send the data along with the html/Javascript.

One way to send the data would be to inject it into the Javascript.

var x={{username}};

Another idea would be to create a meta item in the head and store the data there and then use jQuery (or equivalent) to retrieve it.

Is either of these methods preferable to the other or are there any other methods that I should consider?

  • 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-05-26T12:05:52+00:00Added an answer on May 26, 2026 at 12:05 pm

    Is either of these methods preferable to the other or are there any other methods that I should consider?

    I don’t think there’s a single right answer, and certainly I’ve heard lots of contradictory opinions on this subject, but choosing from the two methods you mention I’d go with injecting the values into JavaScript because I find it easier.

    But since you asked for other suggestions you can kind of combine the two ideas: instead of meta items, your server-side code can just insert a single <script> element in the <head> to store the data in a variable that can then be accessed directly from any of your other scripts (including scripts in multiple source files). You can stick all of your data in a single object to avoid lots of globals:

    <html>
    <head>
    <script>
       var dataFromServer = { /* all your server data here */};
    </script>
    <script type="text/javascript" src="somelibrarycript.js"></script> 
    <script type="text/javascript" src="someotherscript.js"></script>
    <script>
       if (dataFromServer.someProperty) {
          // someProperty provided above so do something with it...
       }
    </script>
    </head>
    etc...
    

    This approach has the added advantage that the server-side code is essentially just producing JSON (that becomes an object literal if included directly in the page source) so if you later decide to start using some Ajax you’ll already be almost there.

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

Sidebar

Related Questions

Often I will have a JavaScript file that I want to use which requires
I quite often see JavaScript with variables that start with a dollar sign. When/why
Often during writing JavaScript, we have to write configuration object. For example Ext.define('User', {
This is a general kind of question. Very often, I need to write JavaScript
JQuery events are annoying me. The thing is that I very often use javascript
Often a user will misuse a Rich Internet Application, and attempt to use the
I often see in third party JavaScript code that after: var el = document.getElementById(elementId);
I often need to load other javascript files via ajax, so at the beginning
I have the impression that the use of javascript often breaks W3C xhtml/css validity.
The users of my Javascript based site often need to create an event where

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.