1. 程式人生 > >JS獲取頁面超連結傳來的引數

JS獲取頁面超連結傳來的引數

<script type="text/javascript">
var url = window.location.href;
function getUrlParam(url,name){
    var pattern = new RegExp("[?&]" + name +"\=([^&]+)","g");
    var matcher = pattern.exec(url);
    var items = null;
    if(matcher != null){
        try{
            items = decodeURIComponent(decodeURIComponent(matcher[1]));   
        }catch(e){
            try{
                items = decodeURIComponent(matcher[1]);
            }catch(e){
                items = matcher[1];
            }
        }
    }
    return items;
}


var imgUrl = getUrlParam(url,'id'); //此處id為url中真實引數名
if(imgUrl.indexOf(".pdf") >= 0 ){
window.location.href=imgUrl;
}else{
var content = "<img src='"+imgUrl+"' />"
$('div').append(content);
}


</script>