Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 阅读url并显示正确的结果_Javascript_Jquery_Url - Fatal编程技术网

Javascript 阅读url并显示正确的结果

Javascript 阅读url并显示正确的结果,javascript,jquery,url,Javascript,Jquery,Url,有人能帮我处理这个代码吗 现在它是这样工作的 如果url是 www.example.com/dc.html?dc=apples 然后展示 "I like Apples" 当url为 www.example.com/dc-Apples.html 表示如果HTMLurl包含apple,则应显示 "I like Apples" 这是我现在的代码。 () <style> .dynamic-content { display:none; } </style>

有人能帮我处理这个代码吗

现在它是这样工作的

如果url是

www.example.com/dc.html?dc=apples
然后展示

"I like Apples"
当url为

www.example.com/dc-Apples.html
表示如果HTML
url
包含
apple
,则应显示

"I like Apples"
这是我现在的代码。 ()

 <style>
    .dynamic-content {
    display:none;
}
</style>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
    // Parse the URL parameter
    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
    // Give the parameter a variable name
    var dynamicContent = getParameterByName('dc');

     $(document).ready(function() {

        // Check if the URL parameter is apples
        if (dynamicContent == 'apples') {
            $('#apples').show();
        } 
        // Check if the URL parameter is oranges
        else if (dynamicContent == 'oranges') {
            $('#oranges').show();
        } 
        // Check if the URL parameter is bananas
        else if (dynamicContent == 'bananas') {
            $('#bananas').show();
        } 
        // Check if the URL parmeter is empty or not defined, display default content
        else {
            $('#default-content').show();
        }
    });
</script>
<!-- Default Dynamic Section -->
<div id="default-content" class="dynamic-content">
  This is the default content
</div>
<!-- Dynamic Section 1 -->
<div id="apples" class="dynamic-content">
  I like apples
</div>
<!-- Dynamic Section 2 -->
<div id="oranges" class="dynamic-content">
  I like oranges
</div>
<!-- Dynamic Section 3 -->
<div id="bananas" class="dynamic-content">
  I like bananas
</div>

.动态内容{
显示:无;
}
//解析URL参数
函数getParameterByName(名称、url){
如果(!url)url=window.location.href;
name=name.replace(/[\[\]]/g,\\$&);
var regex=new RegExp(“[?&]”+name+”(=([^&#]*)和|#|$),
结果=regex.exec(url);
如果(!results)返回null;
如果(!results[2])返回“”;
返回组件(结果[2]。替换(/\+/g,”);
}
//给参数一个变量名
var dynamicContent=getParameterByName('dc');
$(文档).ready(函数(){
//检查URL参数是否为apples
if(dynamicContent=='apples'){
$(“#苹果”).show();
} 
//检查URL参数是否为oranges
else if(dynamicContent=='oranges'){
$(“#橙子”).show();
} 
//检查URL参数是否正确
else if(dynamicContent==‘香蕉’){
$(“#香蕉”).show();
} 
//检查URL参数是否为空或未定义,显示默认内容
否则{
$(“#默认内容”).show();
}
});
这是默认内容
我喜欢苹果
我喜欢桔子
我喜欢香蕉
最佳解决方案 你也可以查一下
根据此解决方案,您必须使用实现的函数获取参数值,并执行任何您想要的操作。

当浏览器向服务器请求
www.example.com/dc Apples.html
URL时,服务器将查找
dc Apples.html
文件。因此,您需要创建
dc Apples.html
文件。因为您的URL是动态的,所以不可能只使用jQuery处理动态URL

为了处理动态URL,您需要使用服务器端编码,如
.php
asp.net
.htaccess

下面我使用
.htaccess
.php
来解决您的问题

In.htaccess

RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+).html dc.php?dc=$1 [NC,L]
在dc.php中

<?php
    if(isset($_GET['dc'])){
        $dc_value = explode("-",$_GET['dc']);
        $dc_value[1] = strtolower($dc_value[1]);
    }else{
        echo "can't get parameter";
        die();
    }
?>

<?php if($dc_value[1] == "apples"): ?>
    <div id="apples" class="dynamic-content">
        I like apples
    </div>
<?php elseif($dc_value[1] == "oranges"): ?>
    <div id="oranges" class="dynamic-content">
        I like oranges
    </div>
<?php elseif($dc_value[1] == "bananas"): ?>
    <div id="bananas" class="dynamic-content">
        I like bananas
    </div>
<?php else: ?>
    <div id="default-content" class="dynamic-content">
        This is the default content
    </div>
<?php endif; ?>

那么您想更改url?检查此项匹配的条件是什么?
example.com/a/bdc Apples.gif
OK吗?到目前为止,您尝试了什么?它只在我键入如下url时起作用:www.example.com/dc.html?dc=apples我要它做的是读取url,如果url包含“apple”,那么在该站点上写“我喜欢苹果”。我会把它放在标题中,这样它就会应用到我所有的页面上,所有包含“苹果”的页面都会显示“我喜欢苹果”。这是一个很好的解决方案。效果很好。谢谢。太好了,如果成功的话,你可以接受我的答案@Charlichtarveri增加了$dc_值[1]=substr(0,6);到dc.php,因此即使它的“appl”,也会显示“我喜欢苹果”,但不起作用。你能帮我一下吗?谢谢。如果条件
谢谢,请尝试更改。当一个链接是www.example.com/jskkskapplesmething.html时,我尝试让它工作,希望脚本能够识别单词apple。现在,如果url仅为Apple,则消息将显示。