0

Can someone help me with this code.

Now it is working like this.

If url is

www.example.com/dc.html?dc=apples

Then show

"I like Apples"

I want to show the result like above, when url is

www.example.com/dc-Apples.html

Means If the HTML url contain apple then it should show

"I like Apples"

Here is the code I have now. ..............................................................................................................................................

 <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>
Hiren Vaghasiya
  • 5,454
  • 1
  • 11
  • 25
  • So you want to change the url? – Albert Einstein May 17 '18 at 04:51
  • Check this https://stackoverflow.com/a/979997 – 11thdimension May 17 '18 at 04:53
  • What are the conditions for matching? Is `example.com/a/bdc-Apples.gif` OK? What have you tried so far? – Ken Y-N May 17 '18 at 04:55
  • It only works when i type the url like this www.example.com/dc.html?dc=apples – Charlie Carver May 17 '18 at 04:58
  • What i want it do is read the url and if the url contain "apple" then write "I like apples" on that site. I will put this in the header so it will be applied to all pages I have, all pages that contain "apples" in the name then "I like apple" should show. – Charlie Carver May 17 '18 at 05:05
  • does your `url` have to be like `www.example.com/dc-Apple.html` ? and if the page (`dc-Apple.html`) does not exist and you are not using any routing mechanisms to handle it than the proposed `url` will not work then your only option would be to either do it by `Query Parameter` or create an additional page (if you don't have any route handling mechanisam). – vikscool May 17 '18 at 05:06

2 Answers2

0

best solution here also you can check this answer according to this solution you must get parameter value with implemented function and doing whatever you want.

Amin Ashtiani
  • 11
  • 1
  • 3
0

When browser request www.example.com/dc-Apples.html URL to the server, then the server will look for dc-Apples.html file. So you need to create dc-Apples.html file. Since your URL is dynamic, it is not possible to handle dynamic URL only with jQuery.

In order to handle dynamic URL, you need to use server-side coding like .php, asp.net, .htaccess

Below I have used .htaccess and .php to solve your question.

In .htaccess

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

In 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; ?>

Now it will works, when you call www.example.com/dc-Apples.html, it will display "I like apples"

LucidKit
  • 28
  • 5