need help making a javascript work

X

Xero

hi. i want to assign different background image to a web page according to
the query string in the URL. so i write this javascript ...


thisURL = window.location;
if (thisURL.indexOf('bio') != -1) {
document.body.background = "bg2.jpg"
}


... only to find that it doesn't work!

what is wrong with it? thanks.
 
M

MD Websunlimited

Hi Xero,

The problem is that you're getting the object "location" versus the URL string which is contained in window.location.href.
 
T

Trevor L.

Xero said:
hi. i want to assign different background image to a web page
according to the query string in the URL. so i write this javascript
...


thisURL = window.location;
if (thisURL.indexOf('bio') != -1) {
document.body.background = "bg2.jpg"
}


... only to find that it doesn't work!

what is wrong with it? thanks.

To retrieve a query string use
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
}

So if you call the page as
thepage.html?bgcol=bg2.jpg
then the JS in thepage.html would be (not tested)
<head>

<script type="text/javascript">
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
}
document.body.background = qsobj(0)
</script>

</head>
 
M

MD Websunlimited

I'm confused how does this have anything to do with the question asked?

--
Mike
http://www.websunlimited.com
FrontPage Add-in
MVP FrontPage '97 - '02

Trevor L. said:
Xero said:
hi. i want to assign different background image to a web page
according to the query string in the URL. so i write this javascript
... thisURL = window.location;
if (thisURL.indexOf('bio') != -1) {
document.body.background = "bg2.jpg"
}


... only to find that it doesn't work!

what is wrong with it? thanks.

To retrieve a query string use
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
}

So if you call the page as thepage.html?bgcol=bg2.jpg then the JS in thepage.html would be (not tested)
<head>

<script type="text/javascript">
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
} document.body.background = qsobj(0)
</script>

</head>
--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
 
M

MD Websunlimited

Never mind I see what you're doing.

--
Mike
http://www.websunlimited.com
FrontPage Add-in
MVP FrontPage '97 - '02

MD Websunlimited said:
I'm confused how does this have anything to do with the question asked?

--
Mike
http://www.websunlimited.com
FrontPage Add-in
MVP FrontPage '97 - '02

Trevor L. said:
Xero said:
hi. i want to assign different background image to a web page
according to the query string in the URL. so i write this javascript
... thisURL = window.location;
if (thisURL.indexOf('bio') != -1) {
document.body.background = "bg2.jpg"
}


... only to find that it doesn't work!

what is wrong with it? thanks.

To retrieve a query string use
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
}

So if you call the page as thepage.html?bgcol=bg2.jpg then the JS in thepage.html would be (not tested)
<head>

<script type="text/javascript">
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\ +/g," ")) : null
} document.body.background = qsobj(0)
</script>

</head>
--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top