1

In this case:

$.get($(this).attr('href'), function(destino){
    //
}

How I can read the elements and which was get from file $(this).attr('href') and save them in a variable to use later?

I try this options, and none work:

1)

$titulo = $(destino).get('title');
$mensagem = $(destino).get('body');

2)

$titulo = destino.getElementByName('title');
$mensagem = destino.getElementByName('body');

3)

$titulo = destino.title;
$mensagem = destino.body;

4)

$titulo = $(destino).attr('title');
$mensagem = $(destino).attr('body');

I try this way and appending .text() and .val() on the end of the sentence. Someone can say how this can be made?

UPDATE

Following the suggestion posted on this topic, I write this code:

$('document').ready(function(){
    $('a').click(function(e){
        if($(this).attr('href') != 'logout.html') {
            e.preventDefault();
            $.get($(this).attr('href'), function(destino){
                var $destino = $(destino);
                $titulo = $destino.find('title').text();
                $mensagem = $destino.find('body').html();
                alert('titulo = '+$titulo+' // mensagem = '+$mensagem)
                BootstrapDialog.show({
                    title: $titulo,
                    message: $mensagem,
                    draggable: true
                });
            });
        }
    });
});

but doesn't working. When I try look at the content form $titulo and $mensagem, seems like both are empty and undefined, respectively.

ANOTHER UPDATE

I made another change. Now I have this code:

$('document').ready(function(){
    $('a').click(function(e){
        if($(this).attr('href') != 'logout.html') {
            e.preventDefault();
            $.get($(this).attr('href'), function(destino){
                var $destino = $(destino);
                $titulo = $destino.filter('title').text();
                $mensagem = $destino.filter('body').html();
                alert('titulo = '+$titulo+' // mensagem = '+$mensagem)
                BootstrapDialog.show({
                    title: $titulo,
                    message: $mensagem,
                    draggable: true
                });
            });
        }
    });
});

the value of $titulo is now correct, but the value for $mensagem not.

10
  • It depends on what destino returns. Commented Mar 19, 2014 at 17:50
  • @LShetty: You can guess from "title" and "body" it is returning a standard HTML page :) Commented Mar 19, 2014 at 17:51
  • Right, the can we also guess the DOM struct? :P and I don't do guess work :) Commented Mar 19, 2014 at 17:52
  • @LShetty: No need to guess... he only wants the title and body tags... Nothing else is specified :P( I find a lot of SO questions require guesswork :) Commented Mar 19, 2014 at 17:53
  • @TrueBlueAussie, Right, an XML struct can also have body and title tags :) Commented Mar 19, 2014 at 17:53

3 Answers 3

3

basically destino is nothing but a string to begin with. You use JQuery to convert it to a DOM structure with $(destino) so that step cannot be avoided.

For starters you only want to wrap destino once, as $(destino) has to convert the entire download into a DOM structure.

e.g.

var $destino = $(destino) so you only perform the costly operation once.

Then var $title = $destino.find('title') and var $body = $destino.find('body') will reference the elements you want.

if you want the text of the title it would be $destino.find('title').text(). If you want the HTML of the body use $destino.find('body').html()

If you want something else you need to be clear about what you intended, as you have supplied 4 ways of not getting data, but no clue to what data you actually want :)

Sign up to request clarification or add additional context in comments.

1 Comment

ok, I update the question with some code I write following your suggestions. The code doesn't work, seems like $title and $message are empty after I try capture their values.
1
$(function(){
    $('a').click(function(e){
        if($(this).attr('href') != 'logout.html') {
            e.preventDefault();
            $.get($(this).attr('href'), function(data){
                var $temp  = $('<div/>', {html:data});
                var $title = $temp.find('title').text();
                var $body  = $temp.remove('head').html();
                console.log( $title +' // '+ $body );
            });
        }
    });
});

Comments

0

In the handler, you are doing a $.get on all of the links in the document. I assume you just want the link that was clicked, which you can get with $(this).

Try this

<script>
    // The ready event handler is called after the page is ready.
    $().ready(function () {
        $('a').click(function(e) {
            e.preventDefault();

            $.get($(this).attr('href'), function(destino) {
                alert('destino = '+destino);
             $titulo = $destino.find('title').text();
             $mensagem = $destino.find('body').html();
                alert('titulo = '+$titulo+' // mensagem = '+$mensagem)
            });

            BootstrapDialog.show({
                title: 'Draggable Dialog',
                message: $mensagem,
                draggable: true
            });
        });
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.