[英]passing the value one html page to another html javascript function
我需要使用javascript代碼將一個html頁面的值傳遞給另一個html javascript函數。 如何傳遞價值。 提前致謝
view.cshtml:
b.on('click', function () {
document.location.href = + '?Id=' + sData; // need to pass the value
});
index.cshtml:
function getId(data) { // i need to get the data here
}
您有兩種選擇:
通過服務器(例如,使用PHP):
view.cshtml:
// assume sData = 5
b.on('click', function () {
document.location.href = + 'index.php?Id=' + sData;
});
當您單擊該函數時,它將從服務器請求index.php。
index.php:
<?php
...
echo "var id = $_POST['Id'];";
echo "function getId() {";
// Function code here refering to id variable
echo "}";
...
?>
index.php生成:
index.cshtml:
var id = 5;
function getId() {
}
您可以通過Javascript檢索GET數據。 我創建了一個使用PHP樣式語法的示例:
<script>
var $_GET = {};
(function() {
var params=top.location.search.split("?").join("").split("&");
for(var i=0;i<params.length;i++){
var param=params[i].split("=");
$_GET[param[0]]=param[1];
}
})();
alert($_GET['Id']);
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.