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?
this won't work in IE6 or older Firefox versions (3.0). use document.title.
artur
2010-03-08 01:37:39
+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
2009-12-13 19:43:03
@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
2009-12-14 04:08:29
I would argue that by that logic you never need jQuery. Also, I don't foresee page title changing to be a bottleneck.
cobbal
2009-12-14 06:08:42
A:
And, don't forget about security... Because, this script is vulnerable to XSS...
B7ackAnge7z
2009-12-14 04:02:56