[jQuery] Re: How to get parts of URL after domain
If you prefer to handle it with jQuery, you could also use the URL parser plugin: http://projects.allmarkedup.com/jquery_url_parser/ . Though, Michael solution is quite elegant.
[jQuery] Re: How to get parts of URL after domain
Steve, now I'm really confused. Does it work like a charm, or do you keep getting an error? (Or both?) :-) BTW, you can write $("td#header") as just $("#header") and it may actually be faster because it will use a direct document.getElementById() lookup. Also, just a suggestion, if you get in the habit of using single quotes for your JavaScript strings instead of double quotes, it will be easier when you have to write some code that requires double quotes *inside* the string, such as code that generates HTML attributes. -Mike > From: Wonder95 > > OK, so if I try using that to set my banner, I come up with > something like this: > > var img = { > '/ops/content/services': 'banner2.jpg', > '/ops/content/services': 'banner3.jpg', > }[location.pathname] || 'banner1.jpg'; > > $("td#header").css("background","url(/ops/sites/all/themes/theme060/images/" + img + ") no-repeat"); > > and it works like a charm. All in 5 lines of code. WOO HOO! Thanks > a ton! > > Steve > > But I keep getting an error > > On Dec 22, 12:50 pm, "Michael Geary" wrote: > > Sure, it's just a combination of some other JavaScript > features that > > may look more familiar if we take them one by one: > > > > // Use an object literal to create an object > > // with two properties. Each property has > > // a name and a value. > > var images = { > > '/services': 'one-image.png', > > '/about-us': 'another-image.png' > > }; > > > > // Select one of the properties from the > > // object by name, and get its value. If > > // not found, the value is undefined. > > // (That doesn't mean an unpredictible > > // value, it means the specific value in > > // JavaScript known as "undefined".) > > var img = images[location.pathname]; > > > > // Select a default image if img is undefined. > > img = img || 'default-image.png'; > > > > // Or another way to do that last statement > > // (means exactly the same thing): > > if( ! img ) > > img = 'default-image.png'; > > > > -Mike > > > > > From:Wonder95 > > > > > Could you explain that construct? I'm no JS expert, and > I haven't > > > seen it before. > > > > > Thanks. > > > > If you want to do it in JavaScript, you don't need > jQuery, regular > > > > expressions, or indexOf. window.location (or just location) has > > > > several properties that give you different pieces of the URL. > > > > location.pathname is the one you want here. Note that it > > > includes the leading slash. For example: > > > > > > var img = { > > > > '/services': 'one-image.png', > > > > '/about-us': 'another-image.png' > > > > }[location.pathname] || 'default-image.png'; >
[jQuery] Re: How to get parts of URL after domain
OK, so if I try using that to set my banner, I come up with something like this: var img = { '/ops/content/services': 'banner2.jpg', '/ops/content/services': 'banner3.jpg', }[location.pathname] || 'banner1.jpg'; $("td#header").css("background","url(/ops/sites/all/themes/ theme060/images/" + img + ") no-repeat"); and it works like a charm. All in 5 lines of code. WOO HOO! Thanks a ton! Steve But I keep getting an error On Dec 22, 12:50 pm, "Michael Geary" wrote: > Sure, it's just a combination of some other JavaScript features that may > look more familiar if we take them one by one: > > // Use an object literal to create an object > // with two properties. Each property has > // a name and a value. > var images = { > '/services': 'one-image.png', > '/about-us': 'another-image.png' > }; > > // Select one of the properties from the > // object by name, and get its value. If > // not found, the value is undefined. > // (That doesn't mean an unpredictible > // value, it means the specific value in > // JavaScript known as "undefined".) > var img = images[location.pathname]; > > // Select a default image if img is undefined. > img = img || 'default-image.png'; > > // Or another way to do that last statement > // (means exactly the same thing): > if( ! img ) > img = 'default-image.png'; > > -Mike > > > From:Wonder95 > > > Could you explain that construct? I'm no JS expert, and I > > haven't seen it before. > > > Thanks. > > > If you want to do it in JavaScript, you don't need jQuery, regular > > > expressions, or indexOf. window.location (or just location) has > > > several properties that give you different pieces of the URL. > > > location.pathname is the one you want here. Note that it > > includes the leading slash. For example: > > > > var img = { > > > '/services': 'one-image.png', > > > '/about-us': 'another-image.png' > > > }[location.pathname] || 'default-image.png';
[jQuery] Re: How to get parts of URL after domain
Sure, it's just a combination of some other JavaScript features that may look more familiar if we take them one by one: // Use an object literal to create an object // with two properties. Each property has // a name and a value. var images = { '/services': 'one-image.png', '/about-us': 'another-image.png' }; // Select one of the properties from the // object by name, and get its value. If // not found, the value is undefined. // (That doesn't mean an unpredictible // value, it means the specific value in // JavaScript known as "undefined".) var img = images[location.pathname]; // Select a default image if img is undefined. img = img || 'default-image.png'; // Or another way to do that last statement // (means exactly the same thing): if( ! img ) img = 'default-image.png'; -Mike > From: Wonder95 > > Could you explain that construct? I'm no JS expert, and I > haven't seen it before. > > Thanks. > > If you want to do it in JavaScript, you don't need jQuery, regular > > expressions, or indexOf. window.location (or just location) has > > several properties that give you different pieces of the URL. > > location.pathname is the one you want here. Note that it > includes the leading slash. For example: > > > > var img = { > > '/services': 'one-image.png', > > '/about-us': 'another-image.png' > > }[location.pathname] || 'default-image.png';
[jQuery] Re: How to get parts of URL after domain
Could you explain that construct? I'm no JS expert, and I haven't seen it before. Thanks. > If you want to do it in JavaScript, you don't need jQuery, regular > expressions, or indexOf. window.location (or just location) has several > properties that give you different pieces of the URL. location.pathname is > the one you want here. Note that it includes the leading slash. For example: > > var img = { > '/services': 'one-image.png', > '/about-us': 'another-image.png' > }[location.pathname] || 'default-image.png'; > > -Mike
[jQuery] Re: How to get parts of URL after domain
Wow. Had never seen that construct, very efficient. Learning something new everyday :) cheers, - ricardo On Dec 21, 10:46 pm, "Michael Geary" wrote: > I would think it would make more sense to do this in PHP instead of > JavaScript. You can use PHP code in your Drupal theme. > > If you want to do it in JavaScript, you don't need jQuery, regular > expressions, or indexOf. window.location (or just location) has several > properties that give you different pieces of the URL. location.pathname is > the one you want here. Note that it includes the leading slash. For example: > > var img = { > '/services': 'one-image.png', > '/about-us': 'another-image.png' > }[location.pathname] || 'default-image.png'; > > -Mike > > > From: Wonder95 > > > I"m sure this is easy to do, but I can't figure it out for > > some reason. I'm writing a simple little function in Drupal > > (in script.js in my theme) to display a different banner > > image (as a background for an element) based on the URL. So, > > for instance, if the URL ishttp://www.mysite.com/services, I > > want to display one image, if it's > >http://www.mysite.com/about-us, I want to display another > > one, and so on. Is there a built in way to get everything > > after "http://www.mysite.com/"; in jQuery, or do I just need > > to use window.location and do something like use a regular > > expression to get what I need? > > > Thanks. > > > Steve
[jQuery] Re: How to get parts of URL after domain
I would think it would make more sense to do this in PHP instead of JavaScript. You can use PHP code in your Drupal theme. If you want to do it in JavaScript, you don't need jQuery, regular expressions, or indexOf. window.location (or just location) has several properties that give you different pieces of the URL. location.pathname is the one you want here. Note that it includes the leading slash. For example: var img = { '/services': 'one-image.png', '/about-us': 'another-image.png' }[location.pathname] || 'default-image.png'; -Mike > From: Wonder95 > > I"m sure this is easy to do, but I can't figure it out for > some reason. I'm writing a simple little function in Drupal > (in script.js in my theme) to display a different banner > image (as a background for an element) based on the URL. So, > for instance, if the URL is http://www.mysite.com/services, I > want to display one image, if it's > http://www.mysite.com/about-us, I want to display another > one, and so on. Is there a built in way to get everything > after "http:// www.mysite.com/" in jQuery, or do I just need > to use window.location and do something like use a regular > expression to get what I need? > > Thanks. > > Steve >
[jQuery] Re: How to get parts of URL after domain
Why not get the url and test it? if (url.indexOf("services") > 0) { do this } else if (url.indexOf("about-us") > 0) { do this } That's one way to do it. http://2whoa.com/dominate --- On Sun, 12/21/08, Wonder95 wrote: From: Wonder95 Subject: [jQuery] How to get parts of URL after domain To: "jQuery (English)" Date: Sunday, December 21, 2008, 5:10 PM I"m sure this is easy to do, but I can't figure it out for some reason. I'm writing a simple little function in Drupal (in script.js in my theme) to display a different banner image (as a background for an element) based on the URL. So, for instance, if the URL is http://www.mysite.com/services, I want to display one image, if it's http://www.mysite.com/about-us, I want to display another one, and so on. Is there a built in way to get everything after "http:// www.mysite.com/" in jQuery, or do I just need to use window.location and do something like use a regular expression to get what I need? Thanks. Steve
[jQuery] Re: How to get parts of URL after domain
if (url.indexOf("services") >0) { --- On Sun, 12/21/08, Wonder95 wrote: From: Wonder95 Subject: [jQuery] How to get parts of URL after domain To: "jQuery (English)" Date: Sunday, December 21, 2008, 5:10 PM I"m sure this is easy to do, but I can't figure it out for some reason. I'm writing a simple little function in Drupal (in script.js in my theme) to display a different banner image (as a background for an element) based on the URL. So, for instance, if the URL is http://www.mysite.com/services, I want to display one image, if it's http://www.mysite.com/about-us, I want to display another one, and so on. Is there a built in way to get everything after "http:// www.mysite.com/" in jQuery, or do I just need to use window.location and do something like use a regular expression to get what I need? Thanks. Steve
[jQuery] Re: How to get parts of URL after domain
if (url.indexOf("services") >0) { --- On Sun, 12/21/08, Wonder95 wrote: From: Wonder95 Subject: [jQuery] How to get parts of URL after domain To: "jQuery (English)" Date: Sunday, December 21, 2008, 5:10 PM I"m sure this is easy to do, but I can't figure it out for some reason. I'm writing a simple little function in Drupal (in script.js in my theme) to display a different banner image (as a background for an element) based on the URL. So, for instance, if the URL is http://www.mysite.com/services, I want to display one image, if it's http://www.mysite.com/about-us, I want to display another one, and so on. Is there a built in way to get everything after "http:// www.mysite.com/" in jQuery, or do I just need to use window.location and do something like use a regular expression to get what I need? Thanks. Steve
[jQuery] Re: How to get parts of URL after domain
I'm not sure what happened there with my multiple replies. Sorry --- On Sun, 12/21/08, Wonder95 wrote: From: Wonder95 Subject: [jQuery] How to get parts of URL after domain To: "jQuery (English)" Date: Sunday, December 21, 2008, 5:10 PM I"m sure this is easy to do, but I can't figure it out for some reason. I'm writing a simple little function in Drupal (in script.js in my theme) to display a different banner image (as a background for an element) based on the URL. So, for instance, if the URL is http://www.mysite.com/services, I want to display one image, if it's http://www.mysite.com/about-us, I want to display another one, and so on. Is there a built in way to get everything after "http:// www.mysite.com/" in jQuery, or do I just need to use window.location and do something like use a regular expression to get what I need? Thanks. Steve