View Full Version : js GET function

william james
02-12-2011, 06:38 AM

I have php file and Im editing the content via url and get function.


<?php echo $_GET['id'];?> << I use that inside the php file

What is the js version of that function?

thank you in advance!

william james
02-12-2011, 06:41 AM
Users can put my contents thru iframe but I want to use a js version instead... been searching for 3 hrs and still get no solution... :(

02-12-2011, 07:42 AM
Nothing standard for that. This works:

function getQval(n) {
if(typeof n !== 'string'){
return null;
var r = new RegExp('[?&;]' + n + '=([^&;#]*)'), m = location.search;
return (m = r.exec(m))? unescape(m[1]) : null;

With that in hand (already available to the page), you can replace:

<?php echo $_GET['id'];?>


<script type="text/javascript">document.write(getQval('id'));</script>

There are of course various other ways to use it. And it will now only work if the client has javascript enabled, so best to keep some form of the PHP version as a fall back. How you implement either/both depends a lot upon what exactly you are trying to do.