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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:16:07+00:00 2026-05-14T04:16:07+00:00

when I look up the syntax for .data(), it gives examples like this: $(‘body’).data(‘foo’,

  • 0

when I look up the syntax for .data(), it gives examples like this:

$('body').data('foo', 52);

I am doing AJAX loads and I was wondering if it is possible to store the incoming html using .data() so once the content is loaded, I would not need to do another AJAX load if the same link is clicked again – I would check to see if the .data key is empty.

Would something like this work?:

To load the contents of a #ajaxdiv into storage:

$('body').data('storage', div#ajaxdiv.html());

To test if the data has already been loaded:

if $('body').data('storage') != '' {

div#ajaxdiv.html($('body').data('storage'));

}

Thanks in advance!!

Al

  • 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-14T04:16:07+00:00Added an answer on May 14, 2026 at 4:16 am

    Edit: After looking at this again, I’m not really sure why you’re storing the data in the body element. Keep in mind that the data() method is not a replacement for all variables– just data that you want associated with a given element. See my example below for more on this.

    It should work fine. The data() method is like storing data into a variable, but that data is now conveniently associated with the element itself.

    You should be careful to namespace the key you use for your data, though. In your code, you’re using 'storage', but that’s awfully generic and seems very susceptible to collisions with other code. I would suggest using a prefix, like: 'blah.storage'.

    Also, SLaks was correct– your selector will need to be passed through jQuery.

    // SLaks' correction and my namespace suggestion
    $('body').data('blah.storage', $('#ajaxdiv').html());
    
    // Alternative method, where #ajaxLink is the link you click to trigger
    // the data retrieval
    $('#ajaxLink').bind('click', function (e) {
        e.preventDefault();
    
        // Notice that we're using $(this).data(), so that our cache is associated
        // with the link element itself.
    
        if ($(this).data('blah.cachedAjaxHtml')) {
            // The data was found in the cache. Use it to populate the div.
            $('#ajaxDiv').html($(this).data('blah.cachedAjaxHtml'));
        } else {
            // Load the data via AJAX and store it in the cache
            var ajaxResult = whateverYouDoToLoadIt();
    
            $(this).data('blah.cachedAjaxHtml', ajaxResult);
            $('#ajaxDiv').html(ajaxResult);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The answer is, that the CollectionViewSource does not initialize its… May 14, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer ^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$ May 14, 2026 at 10:12 pm
  • Editorial Team
    Editorial Team added an answer First, yes you can store a read-only database inside of… May 14, 2026 at 10:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.