3

I have this piece of HTML code.

<div class="tagWrapper">
<i style="background-image: url(https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/390945_10150419199065735_543370734_8636909_2105028019_a.jpg);"></i>
</div>

I need to get that url within the brackets. I tried using the getElementsByClassName() method but it didn't work. Since url is not a HTML element, I have no idea on how to take out the value. I can't use getElementById(), because I can't add an id to the HTML (it's not mine). It needs to work in Chrome and Firefox. Any suggestions?

2
  • What browsers the code has to work with? Commented Dec 17, 2011 at 17:38
  • See this fiddle of my answer: jsfiddle.net/c4urself/8kj7b Commented Dec 17, 2011 at 17:45

7 Answers 7

2

You didn't add a jQuery tag, so here's a native solution (note that this likely won't work on older versions of IE, but you said it only has to work on Chrome and FF):

var origUrl = document.getElementsByClassName("tagWrapper")[0]
                          .children[0].style.backgroundImage;
var url = origUrl.substr(4, origUrl.length - 5);

Or

var url = origUrl.replace("url(", "").replace(")", "");

Here's a fiddle

EDIT

Answering your comment

document.getElementsByClassName("tagWrapper")

gets all elements with the class name tagWrapper. So to get the first one, you grab the zero index

document.getElementsByClassName("tagWrapper")[0]

Then you want the first child under there, and the backgroundImage property on this first child.

document.getElementsByClassName("tagWrapper")[0]
                          .children[0].style.backgroundImage;

From there it's a simple matter stripping the url( and ) from it

var url = origUrl.substr(4, origUrl.length - 5);

or

var url = origUrl.replace("url(", "").replace(")", "");
Sign up to request clarification or add additional context in comments.

3 Comments

+1 This is exactly what the OP asked for, and without jQuery.
This works perfectly! A clever one, I must say. btw can you please explain the first block of code a bit more? sorry but I'm new to JavaScript.
@nK0de - glad you liked it - see my edit for a more thorough explanation.
2

You can use querySelector():

Demo: http://jsfiddle.net/ThinkingStiff/gFy6R/

Script:

var url = document.querySelector( '.tagWrapper i' ).style.backgroundImage;
url = url.substr(4, url.length - 5);

Comments

1

If you where using jquery you could do something like this

$(".tagWrapper i").css("background-image")

Comments

1

I think if you use jQuery it will be easer.

var w = document.getElementsByClassName('tagWrapper')[0];
for (var i=0; i<w.childNodes.length; i++)
  if (w.childNodes[i].tagName && w.childNodes[i].tagName.toLowerCase() == 'i')
    return w.childNodes[i].style.backgroundImage;

4 Comments

Yeah! It's very interesting people don't masster js know how the simple things can became hareder without jquery (or another js framework).
yes, considered that too. The thing is I'm gonna use this to write a greasemonkey/userscript. So as a reference, I have to include ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js But I learned that Chrome doesn't support the @require to do that so that's why I'm trying to accomplish this through HTML only. Thanks for the answer.
@nK0de you can vote an answer up if you think it's good by clicking on the arrow up and if you asked the question you can accept the answer by clicking on the tick.
@micha yes, I will do that definitely right after I finish this one. Thanks.
0
<div class="tagWrapper">
     <i id="something" style="background-image: url(https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/390945_10150419199065735_543370734_8636909_2105028019_a.jpg);"></i>
</div>

// script / without jQuery

var url = document.getElementById('something').style.backgroundImage.match(/\((.*?)\)/)[1];

1 Comment

I cannot add an id because this isn't my HTML code. I wish there was one. It would have been a lot easier.
0

Use jQuery!!!

$("div.tagWrapper i").css("background-image").substr(4, $("div.tagWrapper i").css("background-image").length-5)

Example

3 Comments

Considered jQuery too but the thing is I'm gonna use this to write a greasemonkey/userscript. So as a reference, I have to include ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js But I learned that Chrome doesn't support the @require to do that so that's why I'm trying to accomplish this through HTML only. But thanks for the answer.
You don't need to require it, you can easily copy the code and paste it in-front of the code file. And for your information the newest jQuery version is 1.7.1.
is that so? I didn't know that. I'm new to JavaScript. Still in the learning stage. Thanks a lot for that piece of info.
0

If You don't have to care about Microsoft browsers, the raw JavaScript is quite easy. You can use getElementsByClassName and getElementsByTagName, however it is easier to try querySelectorAll. I've included both. The use of regular expression preserve relative links.

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type='text/javascript'>
    var do_find_a = function() {
        var tmp = document.getElementsByClassName('tagWrapper')[0];
        var tst = tmp.getElementsByTagName('i')[0].getAttribute('style');
        return do_alert(tst);
    }
    var do_find_b = function() {
        var tst = document.querySelectorAll('.tagWrapper i')[0].getAttribute('style');
        return do_alert(tst);
    }
    var do_alert = function(tst) {
        var reg = /background-image:\s*url\(["']?([^'"]*)["']?\);?/
        var ret = reg.exec(tst);
        alert (ret[1]);
        return;
    }
    document.addEventListener('DOMContentLoaded',do_find_a,false);
    document.addEventListener('DOMContentLoaded',do_find_b,false);
</script>
</head>
<body>
    <div class='tagWrapper'>
        <i style='background-image: url("http://example.com/image.jpg");'></i>
    </div>
    Text to ignore.
</body>
</html>

And jsFiddle version:
http://jsfiddle.net/hpgmr/

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.