views:

154

answers:

4

I thought I heard somewhere that you couldn't use jquery to manipulate the content... Is that correct? Basically, I have a site that has parameters like p.php?d=keyword+keyword+keyword, and I wanted to be able to set the title according to what's in that d parameter... Is there a way to do that?

+8  A: 
$('title').text("some text");
cobbal
this won't work in IE6 or older Firefox versions (3.0). use document.title.
artur
+1  A: 

If you really want to do it on the client side with JavaScript/jQuery, try this:

// get query arguments
var $_GET = {},
    args = location.search.substr(1).split(/&/);
for (var i=0; i<args.length; ++i) {
    var tmp = args[i].split(/=/);
    if (tmp[0] != "") {
        $_GET[decodeURI(tmp[0])] = decodeURI(tmp.slice(1).join(""));
    }
}

// change title of document
if (typeof $_GET["d"] == "string") {
    $("title").text($_GET["d"]);
}
Gumbo
+15  A: 

You dont need jQuery for this:

document.title = 'Some text';
David
True, but if you're using jQuery it's good to be consistant.
cobbal
@cobbal Completely disagree. Its better to do what is efficient. Creating a new jQuery object (`$()`), calling an un-scoped `title` search across an entire document, then calling an additional function to set the title is much less efficient than referencing an existing object's property. This is the better way.
Doug Neiner
+1 David for not using jQuery where it isn't needed.
Doug Neiner
I would argue that by that logic you never need jQuery. Also, I don't foresee page title changing to be a bottleneck.
cobbal
A: 

And, don't forget about security... Because, this script is vulnerable to XSS...

B7ackAnge7z