JS/PHP check wheather a url parameter exist
<script>
function isQueryParamPresent(param) {
// Assuming the URL we're checking is the current window location
const queryParams = new URLSearchParams(window.location.search);
// The `has` method returns true if the parameter is present
return queryParams.has(param);
}
console.log( isQueryParamPresent('hk') );
</script>
===================================================
<?php
function isQueryParamPresent($param) {
// Check if the parameter exists in the $_GET array
return isset($_GET[$param]);
}
$paramName = 'hk';
if (isQueryParamPresent($paramName)) {
echo "{$paramName} exists in the URL.";
} else {
echo "{$paramName} does not exist in the URL.";
}
留言
發佈留言