Mike Fielden
Mike Fielden

Reputation: 10153

Grab object from a different page but not using the load method

If a user is on Default for example. Click the navigation to go to Page2 I'd like to load the <div> with the id of container and grab a second <div> with the id of includeScripts, but obviously not load this, just capture the text contained therein.

Example of Default:

<div id="mainContentContainer">
    <!-- Main content container -->
</div>

Example of Page 2:

<div id="container"> 
    Page 2

</div>

<div id="includeScripts">
    Some Text here...
</div>

Current script looks like this:

$navigation = $("#primaryNavigation").delegate('ul li a', 'click', function () {

// Load the container information into the MainContainer div located on Default
$('#mainContentContainer').load($(this).attr('href') + ' #container'); 

// Load the 'includeScripts' div and grab the text

          $.ajax({
            url: $(this).attr('href'),
            context: document.body,
            success: function () {
                console.log('success'); 
            }

    });

I thought maybe using the ajax method jQuery provides would work but apparently not.

If my question is unclear please let me know.

Upvotes: 2

Views: 78

Answers (3)

Mike Fielden
Mike Fielden

Reputation: 10153

This is what I ended up doing... Very similar to Josiah's answer with a minor tweak

$.ajax({
        url: $(this).attr('href'),
        dataType: 'html',
        context: '#mainContentContainer',
        success: function (html) {
                console.log($(html).filter('#includeScripts').text());
                console.log($(html).filter('#includeStylesheets').text());
      }});

Upvotes: 0

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

If I understand your question correctly it should be something like this:

$.ajax({
    url: $(this).attr('href'),
    dataType: 'html',
    context: '#mainContentContainer',
    success: function (html) {
        var text = $(html)
            .find('#container')
            .appendTo(this).end()
            .find('#includeScripts')
            .text();

        console.log(text);
    }
});

Upvotes: 1

Collin Estes
Collin Estes

Reputation: 5799

Why don't you just create a second div on your "Default" page that you will load the text from the "includeScripts" div into, and set that div's display to none.

From there you would be have that script text available in your Default page.

Upvotes: 2

Related Questions