[jQuery] How to parse variables in url to modal box?
How would I parse my variables in a link like...
<a href="index.php?var1=value&var2=value" rel="modalbox">show
modalbox</a>
... to a modal box, like the build-in dialog in jQuery?
I've tried to solve the problem in the code below, but it does not
work for 2 reasons:
1 ) I cannot put in variables using "eval" (see the line "$.get
(anchor...")
2 ) If theres more than one link on the page with the rel-attribute
things gets messed up. The modalbox just shows the first link found on
the page not the one that actually has been clicked.
I really don't want to use "onclick..." if there's another way.
<script type="text/javascript">
$(document).ready(function(){
$("body").append("<div id='modalwindow'></div>");
var anchor = $('a[@rel*=modalbox]');
var url = anchor.attr("href");
var file = url.substring(0, url.indexOf("?"));
var vars = getVarsFromURL(url);
anchor.click(function() {
$("#modalwindow").dialog({
modal: true,
resizable: false,
draggable: true,
width:600,
height:600,
overlay: {
opacity: 0.5,
background: "black"
}
});
$.get(anchor.attr("href"), { eval(vars) }, function(data){
$("#modalwindow").html(data);
});
return false;
});
}); // eval(getVarsFromURL(link.attr("href")))
function getVarsFromURL(url) {
var queryStr = url.substring(url.indexOf("?")+1);
var strRes = "";
var pairs = queryStr.split("&");
for (var i in pairs) {
var pair = pairs[i].split("=");
strRes += pair[0] + ":'" + pair[1] + "',";
}
return strRes.substring(0, strRes.length-1);
}
</script>