PDA

View Full Version : Resolved DD Mega Menu and "safe mail" script causing issue?



neocharles
12-21-2015, 07:05 PM
Script: DD Mega Menu
http://www.dynamicdrive.com/dynamicindex1/ddmegamenu.htm

I am nearly complete with getting my new Navigation Bar set up to deploy on my website. However, there's one thing I am having an issue with (and if the method I am using currently completely sucks, and there's some other solution to give me similar results - I am all ears!)

Right now, we use a script to help prevent some spam from being sent to our email addresses:


<!--
function safemail(name, domain, display) {
displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
}
// -->

We call that safemail.js

So within our menu we have some links you can click on to send emails (you can see the current at https://w4bfb.org/index_w4bfb.php under "Contact" > "Email Contacts" at the far right). we simply put the following code currently:


<li><script type="text/javascript">safemail("board","w4bfb.org","* All Board Members")</script></li>

However, when I add that to the new DD Mega Menu I am setting up, the results are seen on https://w4bfb.org/index_new.php - it does some weird redirect or something and pretty much and shows only that link to send the email. The source, you can see, has the whole website there though.

Is there something obvious I can do to change / fix this, or maybe another anti-spam method you can recommend that won't cause issues with the new menu?


Please ignore any SSL errors you may see - the webhost is rolling out new certs and stuff, so I am trying to get this project finished up so I can properly troubleshoot all they are doing :)

[Edit]

I have tried different variables in the js and it did the same thing, so I do not believe it has to do with the actual variables used. Here is the code of the chunk causing issues:

5765

Beverleyh
12-21-2015, 08:20 PM
I see you have a PHP page so how about a PHP function? Here's one I use quite a bit - it converts random characters to HTML character codes;

function getObfuscatedEmailAddress($email) {
$alwaysEncode = array('.', ':', '@');
$result = '';
for ($i = 0; $i < strlen($email); $i++) { // Encode string using oct and hex character codes
if (in_array($email[$i], $alwaysEncode) || mt_rand(1, 100) < 25) { // Encode 25% of characters including several that always should be encoded
if (mt_rand(0, 1)) {
$result .= '&#' . ord($email[$i]) . ';';
} else {
$result .= '&#x' . dechex(ord($email[$i])) . ';'; }
} else {
$result .= $email[$i]; }
}
return $result;
}

function protectEmail($email, $text = null, $params = array()) {
if (!is_array($params)) { $params = array(); }
if (!isset($params['rel'])) { $params['rel'] = 'nofollow'; }
$neverEncode = array('.', '@', '+'); // Don't encode - not fully supported by IE & Chrome
$urlEncodedEmail = '';
for ($i = 0; $i < strlen($email); $i++) {
if (!in_array($email[$i], $neverEncode) && mt_rand(1, 100) < 25) { // Encode 25% of characters
$charCode = ord($email[$i]);
$urlEncodedEmail .= '%';
$urlEncodedEmail .= dechex(($charCode >> 4) & 0xF);
$urlEncodedEmail .= dechex($charCode & 0xF);
} else {
$urlEncodedEmail .= $email[$i];
}
}
$obfuscatedEmail = getObfuscatedEmailAddress($email);
$obfuscatedEmailUrl = getObfuscatedEmailAddress('mailto:' . $urlEncodedEmail);
$link = '<a href="' . $obfuscatedEmailUrl . '"';
foreach ($params as $param => $value) {
$link .= ' ' . $param . '="' . htmlspecialchars($value). '"';
}
$link .= '>' . $text . '</a>';
return $link;
}
usage
<?php echo protectEmail('me@email.com', 'Email Me'); ?>

neocharles
12-21-2015, 09:07 PM
Hmmm, I can give it a shot, but when I try to use it (in quickly testing it), it seems to break the rendering of other parts of the website.

My other option is to finally break down and create contact forms. Which probably is the better option.

Beverleyh
12-21-2015, 09:35 PM
when I try to use it (in quickly testing it), it seems to break the rendering of other parts of the website.Shouldnt do, but I'll separate the code to make usage clearer. How are you applying it? It's a PHP function so firstly, it should go right at the top of the page (or ideally in a functions include file) in PHP tags; <?php at the start and ?> at the end. It should only appear once in the page/file.

Then when you want to use it, just do <?php echo protectEmail('me@email.com', 'Email Me'); ?>, unless it's within more PHP code, in which case you omit the start and end PHP tags.

neocharles
12-21-2015, 09:40 PM
Shouldnt do, but I'll separate the code to make usage clearer. How are you applying it? It's a PHP function so firstly, it should go right at the top of the page (or ideally in a functions include file) in PHP tags; <?php at the start and ?> at the end. It should only appear once in the page/file.

Then when you want to use it, just do <?php echo protectEmail('me@email.com', 'Email Me'); ?>, unless it's within more PHP code, in which case you omit the start and end PHP tags.

I've tried a few ways of including it ... but it seems to only load the header (you can see it live now). Not sure what is causing that at the moment. (I'll admit - I am definitely not an expert whatsoever. This has been a great learning experience for me taking over the website)

I have created a file, safemail.php, and within it I have

<?php
function protectEmail($emailaddress, $text = null, $params = array()) {
if (!is_array($params)) { $params = array(); }
if (!isset($params['rel'])) { $params['rel'] = 'nofollow'; }
$neverEncode = array('.', '@', '+'); // Don't encode - not fully supported by IE & Chrome
$urlEncodedEmail = '';
for ($i = 0; $i < strlen($emailaddress); $i++) {
if (!in_array($emailaddress[$i], $neverEncode) && mt_rand(1, 100) < 25) { // Encode 25% of characters
$charCode = ord($emailaddress[$i]);
$urlEncodedEmail .= '%';
$urlEncodedEmail .= dechex(($charCode >> 4) & 0xF);
$urlEncodedEmail .= dechex($charCode & 0xF);
} else {
$urlEncodedEmail .= $emailaddress[$i];
}
}
$obfuscatedEmail = getObfuscatedEmailAddress($emailaddress);
$obfuscatedEmailUrl = getObfuscatedEmailAddress('mailto:' . $urlEncodedEmail);
$link = '<a href="' . $obfuscatedEmailUrl . '"';
foreach ($params as $param => $value) {
$link .= ' ' . $param . '="' . htmlspecialchars($value). '"';
}
$link .= '>' . $text . '</a>';
return $link;
}
?>

Then in the index_new.php I have

<?php include './newnavbar/newnavbar.php; './ssi/safemail.php'; ?>

Then within the menu bar item I have

<li><?php echo protectEmail('board@w4bfb.org', 'Board Members'); ?></li>

Beverleyh
12-21-2015, 09:58 PM
My bad - there are 2 functions and I hadn't copied the first one. I've modified the code now so that both are included

neocharles
12-21-2015, 10:01 PM
Updated, but still have the same result. Even when you hover over Contact Us > Email Contacts, that window is blank (even though there should be things in there). Something's not jiving right somewhere :)

Beverleyh
12-21-2015, 10:11 PM
Ack! I'm a bit stuck here because I'm on iPhone/iPad until I get back in the office in January so unfortunately I can't look any deeper for you :(

neocharles
12-21-2015, 10:13 PM
Ack! I'm a bit stuck here because I'm on iPhone/iPad until I get back in the office in January so unfortunately I can't look any deeper for you :(

It's not a problem. Looking at the source, it pretty much seems to go blank right after the <li> where it should begin the <?php stuff

neocharles
12-21-2015, 10:16 PM
Interestingly, if I add the code you provided to the top of newnavbar.php it seems to execute properly... I think.

Only downside I see at a glance right now is it has the down arrow next to the email address, since it uses the rel="nofollow".

neocharles
12-21-2015, 10:34 PM
Got that fixed, can tweak it later.. but in the .css file:

.mega a[rel="nofollow"]:after{ /*add mail logo to anchor links using nofollow, */
content:" \2709";
}
This will work only if "nofollow" is the only attribute (which, in my case, it is). However, more can be read http://www.w3.org/TR/css3-selectors/#attribute-representation

Beverleyh
12-21-2015, 10:45 PM
I copied what I could from the demo page and setup a few email links here http://fofwebdesign.co.uk/template/_testing/menus/ddmegamenu/index.php. JavaScript dropdown in the 1st demo, and the top of the 2nd demo dropdown. Seems to work OK.

You can remove rel="no follow" from the PHP function if you like, by commenting out this line
// if (!isset($params['rel'])) { $params['rel'] = 'nofollow'; }
Or, add a class to the email link
$link = '<a class="email" href="' . $obfuscatedEmailUrl . '"'; and hide the arrow against the email link in the megamenu CSS by adding this;
.mega a.email[rel]:after{display:none}