Use of variables - javascript

I am new on web coding. I use weebly drag and drop but also HTML to add extra things. What I did is has created a variable that includes customers' selections about a product. I would like to add this variable.value to the specific products' short description.
Any help will be appreciated.
Here is my code for to get this variable from another page so final step is to get this variable from product page, short-description section.
Thanks
Tim
<html> <body> <script>
var queryString = decodeURIComponent(window.location.search);
queryString = queryString.substring(1);
var queries = queryString.split("&");
for (var i = 0; i < queries.length; i++) {
document.write(queries[i] + "<br>");
}
</script> </body> </html>

If you already have the variable set, just use this in your script
document.getElementById("TheIdOfTheDiv").innerHTML = TheVariable;

The below js fiddle link might help you to easily decode the url
https://jsfiddle.net/bachiavinash/rc3LsjLo/
js code:
function dispnameval(){
var $input = document.f1.t1.value;
var $output = "";
var $inurl = $input.split("?");
var $ary = $inurl[1].split("&");
var $arylen = $ary.length;
var $start = $input.indexOf("?");
var $end = $input.indexOf("=");
//var $output = $input.substring($start,$end);
//$output = $ary[0];
for($i=0; $i<$arylen; $i++){
//alert($ary[$i]);
$output+=$ary[$i]+"<br/>";
}
document.getElementById("output").innerHTML=$output;
}
function clearnameval(){
document.f1.t1.value = "";
document.getElementById("output").innerHTML="";
}

Related

Passing value of check boxes to Javascript from HTML embedded in PHP

Good afternoon. I am trying to pass the values associated with a list of checkboxes to a javascript function that then calculates the total for all values associated with selected checkboxes.
The HTML code is embedded within PHP. When I try to call the javascript function, I get a null result. Could anyone provide some pointers on where I am going wrong. Code below. All help is appreciated! Thank you.
The variables are pulled from the results of an SQL query executed just before the copied code below.
"<form action = 'book.php' method = 'POST'>
<tr><td>$seat</td>"
."<td>$zone</td>"
."<td>$price</td>"
."<td><input type = 'checkbox' id = 'seat['$seat']' value = '$price'></td></tr>"
//."<input type = 'hidden' id = '$seat' value = '$price' action = 'checkDetails()'>"
;
}
echo "</table>"
."Email address for booking ".
"<input type = 'text' name = 'email id'>"
."  "
."<button type = 'submit' name = 'book' action = 'book.php'>Book</button></form>"
."  "."<button name = 'check price' onclick = 'checkDetails()'>Check Prices</button>";
}
catch (PDOException $e) {
echo "PDOException: ".$e->getMessage();
}
echo "<script language = 'javascript'>
function checkDetails() {
var seat = document.forms[0];
var i;
for (i = 0; i < seat.length; i++) {
if (seat[i].checked) {
var total = document.getElementById('seat[i]');
alert('Your total is ' + total);
}
}
}
</script>";
You are looping through the forms length.
Since this is rendered in php why dont you set the length of of I programmatically
var i;
var length = <?= $seat?>;
for (i = 0; i < length; i++) {
if ( document.getElementById('seat[i]').checked) {
var total = document.getElementById('seat[i]');
alert('Your total is ' + total.value);
}
the following code is looping through the form and not the elements
var seat = document.forms[0];
var i;
for (i = 0; i < seat.length; i++) {

Javascript email encryption, am I doing this right?

I came up with a way to encrypt email addresses using javascript, but seeing as I am new to programming, I'm not sure how effective my solution is. Basically I'm pulling in an email using Advanced Custom Fields, saving a spam-proofed version of it, then transforming it with javascript in the footer. FYI I am using Wordpress.
Here is the relevant part of the page:
<?php get_header(); ?>
<a href='' id='email'></a>
<script type="text/javascript">
var email_storage = "nospam<?php the_field('team-email'); ?>";
console.log(email_storage);
</script>
<?php get_footer(); ?>
Then this script loads in the footer (after jQuery).
var email_length_nospam = email_storage.length;
var email = email_storage.substr(6,email_length_nospam);
var email_length = email.length;
var at = email.indexOf('#');
var email_name = email.substr(0,at);
var email_domain = email.substr((at+1),email_domain);
console.log(email_length_nospam,email);
jQuery('a#email').html(email_name+'#'+email_domain);
jQuery('a#email').attr('href','mailto:'+email);
My question is this:
Am I missing anything obvious? In other words, would a spam bot easily decode this due to an error on my part?
You could check the generated Javascript from this email link obfuscator generator. For example, this is what gets generated if info#example.com is used for both email address and link text:
{ coded = "DMUm#WyBVJgW.AmV"
key = "RGS5Amu4aKOHnZIix0TzqV8EBle1MQPDyhrkNfCp3gWXdjFbwvcosU9JY726Lt"
shift=coded.length
link=""
for (i=0; i<coded.length; i++) {
if (key.indexOf(coded.charAt(i))==-1) {
ltr = coded.charAt(i)
link += (ltr)
}
else {
ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length
link += (key.charAt(ltr))
}
}
document.write("<a href='mailto:"+link+"'>info#example.com</a>")
}
I'm not sure how effective it is though.

Assign Value of Custom Wordpress Post/Page Meta Field to JavaScript Variable

Is there a way via some lines of JavaScript to assign the value of a custom meta field in a Wordpress post/page to a JavaScript variable?
In other words, I've got a custom meta field in all my Wordpress posts and pages named "customamznsearch". I'd like to assign the value of that field to a JavaScript variable with the same name... or different name if need be.
Also, an added bonus would be to also define a static value for the variable if no data is available from that meta field.
This is the code that will be utilizing the "customamznsearch" variable.
<script type="text/javascript">
amzn_assoc_placement = "adunit0";
amzn_assoc_tracking_id = "livcouintheci-20";
amzn_assoc_ad_mode = "search";
amzn_assoc_ad_type = "smart";
amzn_assoc_marketplace = "amazon";
amzn_assoc_region = "US";
amzn_assoc_textlinks = "";
amzn_assoc_linkid = "0c1ce8995df23ae16ec99d3bb32502ec";
amzn_assoc_default_category = "SportingGoods";
amzn_assoc_default_search_phrase = customamznsearch;
</script>
<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US"></script>
This code will be displayed in an Enhanced Text Widget in the footer of my page. The Enhanced Text widget should be fully capable of supporting Text, HTML, CSS, JavaScript, Flash, Shortcodes, and PHP.
After much research, the following snippet of code was what ended up working (please note that I decided to change the variable to 'amazonadserach'):
var amazonadsearch = "<?php global $post;
$amazonadsearch = get_post_meta($post->ID, 'amazonadsearch', true);
echo $amazonadsearch; ?>";
You can just output javascript code from php like this:
<script>
var customamznsearch = "<?php echo addcslashes($customamznsearch, '"'); ?>";
</script>
Or if you mean to get it from the <meta> tags, then:
function getMeta(metaName) {
var metas = document.getElementsByTagName('meta');
for (i=0; i<metas.length; i++) {
if (metas[i].getAttribute("name") == metaName) {
return metas[i].getAttribute("content");
}
}
return "";
}
var twitterTitle = getMeta("twitter:title");

How to send html email line by line using google apps script and google sheet data

I have a script to send an email line-by-line. The problem I have is that I cannot use the data in the current line iteration in an email template. I know how to call functions from the template though in this case I cannot call the function that is being evaluated.
My solution was to create another function that would loop through the data again and send the line instance in an email then break. The problem is that now it takes to long to loop through the lines twice when im sure it can be done once.
I hope i made sense.
Code.gs
function sendMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('EmailList');
// get the data as an array
var data = sheet.getDataRange().getValues();
// Loop over each line
for (var i = 1; i < data.length; i+=1) {
// Check the status of the email
if (data[i][4] != 'Sent') {
// get html file content
var html = HtmlService.createTemplateFromFile('Index').evaluate().getContent();
// send the email
MailApp.sendEmail({
to: data[i][2],
subject: 'Hi ' + data[i][0],
htmlBody: html
});
// Set the status to sent
sheet.getRange(i + 1,5).setValue('Sent');
}
} // end for
} // end function sendMail
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('EmailList');
// get the data as an array
var data = sheet.getDataRange().getValues();
// Loop over each line
for (var i = 1; i < data.length; i+=1) {
// Check the status of the email
if (data[i][4] != 'Sent') {
var firstName = data[i][0];
var lastName = data[i][1];
var email = data[i][2];
var message = data[i][3];
break;
}
} // end for
var returnData = [firstName, lastName, email, message];
return returnData;
} // end function getData
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<? var data = getData(); ?>
</head>
<body>
Hi <?= data[0];?> <?= data[1];?> , with email address <?= data[2];?>
I have a message for you:
<?= data[3];?>
</body>
</html>
You need to read this pushing variables to templates
Your Code.gs
for (var i = 1; i < data.length; i+=1) {
// Check the status of the email
if (data[i][4] != 'Sent') {
var firstName = data[i][0];
var lastName = data[i][1];
var email = data[i][2];
var message = data[i][3];
var returnData = [firstName, lastName, email, message];
var html = HtmlService.createTemplateFromFile('Index');
html.data = returnData ;
var template = html.evaluate().getContent();
// send the email
MailApp.sendEmail({
to: data[i][2],
subject: 'Hi ' + data[i][0],
htmlBody: template
});
// Set the status to sent
sheet.getRange(i + 1,5).setValue('Sent');
}
Your Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hi <?= data[0];?> <?= data[1];?> , with email address <?= data[2];?>
I have a message for you:
<?= data[3];?>
</body>
</html>
Here is the strategy I would use in order to cut down on the processing time. First get all the data that you want to inject into the HTML. Then get the HTML from the file just once, and put it outside of the for loop. Then construct new HTML to be injected into the original HTML for every loop. You will need to use string formulas, and concatenate text to dynamically change the HTML.
You won't look up the data from the spreadsheet on every loop, or have the templated HTML evaluate the HTML on every loop. You'll get the data once. It will be store in an array. You'll get the HTML once, and it will be stored in a variable.
You'll need to use JavaScript string methods to manipulate the HTML.

Modify Breadcrumbs JavaScript to pull from H1 Tags instead of the URL? Desperate!

So the structure of this website I've been told to work on is awful, the url turns into a long line after you go into one section of the website (it's using an external program to import information, so it gets wonky)
function breadcrumbs() {
sURL = new String;
bits = new Object;
var x = 0;
var stop = 0;
var output = "home » ";
sURL = location.href;
sURL = sURL.slice(8,sURL.length);
chunkStart = sURL.indexOf("/");
sURL = sURL.slice(chunkStart+1,sURL.length)
while(!stop){
chunkStart = sURL.indexOf("/");
if (chunkStart != -1){
bits[x] = sURL.slice(0,chunkStart)
sURL = sURL.slice(chunkStart+1,sURL.length);
} else {
stop = 1;
}
x++;
}
for(var i in bits){
output += "" + bits[i] + " » ";
}
document.write(output + document.title);
document.write("");
}
Using this script I was able to get the breadcrumbs navigation to work however it pulls from the URL, can this be modified to pull from the H1 tags of the current page?
SO appreciated if anyone can help!
document.getElementByTagName('h1');
This will get the tag but I can't see how you would build a breadcrumb trail from a single heading. You should ask this on StackOverflow.

Categories

Resources