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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:16:35+00:00 2026-05-17T22:16:35+00:00

According to this answer , it is better to use the $.ajax() function: $(‘.showprayer’).click(

  • 0

According to this answer, it is better to use the $.ajax() function:

$('.showprayer').click( function(event) {
  $.ajax({
    url: '/show_prayer',
    type: 'POST',
    data { 'name' : $(this).text() }, 
    dataType: 'script'
  });
});

rather than the $.post() function:

$('.showprayer').click( function(event) {
  $.post('/show_prayer', { 'name' : $(this).text() }, function(data) {
    eval(data);
  });
});

The reason given is:

The difference is that this uses `jQuery.globalEval()` which sticks the script 
in the <head> as if it were a normal GET requested <script>, instead of actually 
calling `eval()` on it.

My question is, if this is correct, why is it important for the script to end up in the ? Does that make a big difference over using eval()?

  • 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-17T22:16:35+00:00Added an answer on May 17, 2026 at 10:16 pm

    The difference in your examples has nothing to do really between .post() and .ajax(), it’s just about built-in JS eval() vs. jQuery.globalEval(). These differences are covered here:

    http://api.jquery.com/jQuery.globalEval/

    but they don’t do a great job of explaining why it matters. Here’s the issue: if you do just do an ordinary eval of a line like:

     var foo = 5;
    

    from inside a .post() call, that “foo” variable will only live inside that call. If you later do anything else on that page, that variable will be gone, because it was scoped to the anonymous that you passed in to your “post”. In contrast, the jQuery “globalEval” method will make that foo variable a property of window, ie. it will be in the global scope. This means that any other function you subsequently run will still have access to it.

    (Side Note: Putting lots of variables in the global scope isn’t actually always a good idea, because of potential naming conflicts, so the globalEval isn’t always better.)

    In any case, you don’t have to stop using post to get the benefits of your returned script’s variables being global. Just do:

    $.globalEval(data);
    

    in your post function instead of “eval” and you’ll be all set.

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

Sidebar

Related Questions

According to the answer to this question , there is no way to effectively
I have a bad feeling that the answer to this question is no, but
If givin some situation that you can do a loop of a certain event
To extend the User object with custom fields, the Django docs recommend using UserProfiles
Faulty code: pos_1 = 234 pos_n = 12890 min_width = len(str(pos_n)) # is there
Example dump from the list of a directory: hello:3.1 GB world:1.2 MB foo:956.2 KB
What are the pros and cons of using System.Security.Cryptography.RNGCryptoServiceProvider vs System.Random . I know
I need a timer tick with 1ms resolution under linux. It is used to
The behavior described here appears to now be the default for ASP.NET MVC 2
I have a classifieds website. In the main page (index) I have several form

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.