0

first time posting! I'll try to be as clear as possible.

I have a Drupal 6 website and am using a custom template file to render some pages differently than the rest of the website.

This is what the code I'm working with looks like:

<?php print $header; ?> 
<?php if ($title): ?><h1><?php print $title; ?></h1><?php endif; ?>
<?php print $content ?>

The page renders correctly when the URL looks like this: http://example.com/somepage/?format=kiosk

Except any link that is rendered in the page will go to: http://example.com/otherpage/

What I need dynamically appended to the end of any URL is:

?format=kiosk

Is there a to process these links using PHP or JavaScript, without resorting to .htaccess (though that's an option), to add that bit to the URL?

I suppose this could also be handy for other things, like Google Analytics.

Thanks!

Jon

jaruzek
  • 1
  • 2
  • in js with jquery, it's trivial. for php, you'd have to mod every single place a link is generated/output, since php itself has no clue what it's doing. it may be "the" html templating engine, but it still has no clue what html (or links) are. it's just outputting text that happens to have a bunch of `<>` chars in it. – Marc B Aug 06 '15 at 16:00
  • 1
    PS: If you ever want to be able to read your PHP code dont put `` on every line. Start a code section with `` – RiggsFolly Aug 06 '15 at 16:27
  • @RiggsFolly - I am following the syntax of the Drupal theme I'm using. Thanks for informing me of this best practice (I don't code PHP). – jaruzek Aug 07 '15 at 19:25

3 Answers3

0

Sure, just gather all elements with a href attribute and tack it on from there.

var links = document.querySelectorAll('[href]');
for (var i = 0; i < links.length; i++) {
  links[i].href += '?format=kiosk';
}

You'll probably want to do a simple check first to see if the link contains a ? (links[i].href.indexOf('?') > -1) but this will make sure that every href ends in ?format=kiosk.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0
window.history.pushState(window.location.href, "Title", "?format=kiosk");

This will get the current url and append a string you specify at the end

AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
0

Here is a solution using jQuery which runs a quick regex check to make sure the anchor tag is a link to a page on http://example.com. It also checks to see if the link already has a ? or not.

var $links = $('a'); // get all anchor tags

// loop through each anchor tag
$.each($links, function(index, item){
    var url = $(this).attr('href'); // var for value of href attribute
    // check if url is undefined
    if(typeof url != 'undefined') {
        // make sure url does not already have a ?
        if(url.indexOf('?') < 0) {
            // use regex to match your domain
            var pattern = new RegExp(/(example.com\/)(.*)/i);
            if(pattern.test(url))
                $(this).attr('href', url + '?format=kiosk'); // append ?format=kiosk if url contains your domain
        }
    }
});
Jon
  • 261
  • 1
  • 6
  • Thanks for all the replies everyone. I'm a little lost on implementing these. I've tried the jQuery solution (loading through Google's hosted libraries) though I haven't been able to get it to work. I replaced example.com with my own domain...is that all I need to do? What am I missing? Is it because I'm developing on a subdomain? EDIT: disregard my last comment. I put the code before

    and it works! Thanks so much :)

    – jaruzek Aug 07 '15 at 16:51
  • No problem, mark it as an answer if it solved your problem so it can help others with similar problems in the future. – Jon Aug 07 '15 at 17:10