0

I have a code like this:

document.body.innerHTML=data();

I would like to do something (url/split) after this code has finished:

$(data).find('a').prop('href', function(_, href){
    url = href.split('/');          
    return href.replace(url[2], 'someUrl');

}); 

How do I chain the two codes together? I tried:

document.body.innerHTML=data().end().find('a').prop('href', function(_, href){
    url = href.split('/');          
    return href.replace(url[2], 'someUrl');

})

doesn't work, tried some other variations with no luck.

4
  • 1
    Why are you trying to chain them in the first place? I can tell you how but it just makes your code inconsistent. Commented Feb 19, 2013 at 21:51
  • run the jquery inside document.ready Commented Feb 19, 2013 at 21:52
  • Please clarify what data is and provide any other related functions (in case data is a function) Commented Feb 19, 2013 at 22:01
  • Hi all, data is a complete website (<html></html>) which Im screen scraping. Im trying to attach the Jquery split/replace to the plain javascript. Commented Feb 19, 2013 at 22:06

1 Answer 1

1

Maybe try:

document.body.innerHTML = $(data).find('a').prop('href', function(_, href){
    url = href.split('/');          
    return href.replace(url[2], 'someUrl');
}).wrap("<div>").parent().html();

I'm not exactly sure of the difference between your data() and $(data) because you mix them but they are totally different.

Anyways, the result of .prop(name, func) is the selected element from before. So when your last call in the chain is .prop(name, func), you're trying to set document.body.innerHTML as a jQuery object...basically: document.body.innerHTML = $("element");. But I think you're trying to get the datas HTML, so that's what the extra chaining I added should do.

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

4 Comments

Doesn't seem to work for me..don't know why. I get only one insignificant img from the website Im screen scraping(=data)
I already had this code in the past, something like this: $('html').append(data).find('a').prop.....and so one. Did not work for me with some websites so I switch to plain javascript document.body.innerHTML which was better but now Im missing the split function...
Well $('html').append(data) is already a bad idea. You shouldn't be appending elements on the <html> tag...do it with the <body> tag. That's why document.body works "better" - it should've been $("body") in the first place. Also, I'm not sure you can append a full HTML document into the <body> element. You'd need to get the <body>'s contents and only append that. Anyways, can you please update your question with more of your jQuery code that this applies to? And also provide a complete (simplified) example of what would be in the data variable?
document.documentElement.innerHTML seems to work thanks for pointing in the right direction.

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.