i have a fake visitor's counter script which code in javascript but i want to use it in smarty tpl file i am try to do it but its not displaying where i want. the script code is below
<!--Simply copy and paste it where you wish the counter to appear.-->
<SCRIPT language="JavaScript" type="text/javascript">
// counter - from http://rainbow.arch.scriptmania.com/scripts
function fakecounter(){
//decrease/increase counter value (depending on perceived popularity of your site!)
var decrease_increase=2460
var counterdate=new Date()
var currenthits=counterdate.getTime().toString()
currenthits=parseInt(currenthits.substring(2,currenthits.length-4))+decrease_increase
document.write("You are visitor # <b>"+currenthits+"</b> to my site!")
}
fakecounter()
</script>
and i am trying to using it in after </script> .
This script should work without a problem. If you put it in clean Smarty template file you get information similar to:
You are visitor # 945155 to my site!
However in older versions of smarty you need to use {literal} to use JavaScript, so your code should look like this:
<!--Simply copy and paste it where you wish the counter to appear.-->
<SCRIPT language="JavaScript" type="text/javascript">
{literal}
// counter - from http://rainbow.arch.scriptmania.com/scripts
function fakecounter() {
//decrease/increase counter value (depending on perceived popularity of your site!)
var decrease_increase = 2460
var counterdate = new Date()
var currenthits = counterdate.getTime().toString()
currenthits = parseInt(currenthits.substring(2, currenthits.length - 4)) + decrease_increase
document.write("You are visitor # <b>" + currenthits + "</b> to my site!")
}
fakecounter()
{/literal}
</script>
Related
I am currently using this code:
var wordRandomizer = {
run: function (targetElem) {
var markup = this.createMarkup();
targetElem.appendChild(markup);
},
createMarkup: function () {
var that = this;
var frag = document.createDocumentFragment();
this.elem = document.createElement('span');
var button = document.createElement('button');
button.innerText = 'Change Item';
button.addEventListener('click', function () {
that.changeItem();
});
frag.appendChild(this.elem);
frag.appendChild(button);
return frag;
},
changeItem: function () {
var rand = this.getRandInt(1, this.items.length) - 1;
console.log(rand);
this.elem.innerText = this.items[rand];
},
getRandInt: function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
},
items: ['itemA', 'itemB', 'itemC', 'itemD']
};
wordRandomizer.run(document.body);
I code is a button which when pressed grabs one of the items in the list. However, I don't want the items to show on the same page as the generator as people simply look at the source code. How can I make it so once the button is pressed it grabs the random item from another location where people cannot view them all using the source code.
If it helps, you can see the code in action here - http://jsbin.com/ESOdELU/1/edit
I will give you a solution using PHP since it is a free scripting language and is the most likely to be supported by a host or default web server...
For starters, here is the code to include jquery and the basic AJAX script
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function(){
$("#generate").click(function(){
$("#madlibs p").load("script.php");
});
});
</script>
Here is the code for script.php
<?php
header("Cache-Control: no-cache");
// For testing you can use an inline array like the lines below
// Just remove the comment slashes "//" from the beginning of the line
// and comment out the external declarations
//$actors = array('Denzel Washington','Michael J. Fox','Jim Carey','Boris Kodjoe');
//$roles = array('Mental Patient','Homeless Musician','Drag Queen Detective','Tormented Mathematician');
// In production, you would put these in a text file or a database.
// For $actors, put an entry on each line of a text file and save it as 'leads.txt'
// Do the same with a separate file for $roles (roles.txt).
$actors = file("leads.txt");
$roles = file("roles.txt");
// This selects a random element of each array on the fly
echo $prefixes[rand(0,count($actors)-1)] . " stars as a "
. $suffixes[rand(0,count($roles)-1)] . " in the next blockbuster film.";
// Example output:
// Michael J. Fox stars as a Tormented Mathematician in the next blockbuster film.
?>
Put this in the body of your page and be sure to style everything up for display.
<body>
<div id="madlibs"><p> </p></div>
<button id="generate">Surprise Me!</button>
</body>
A couple of notes:
- You can include your basic layout HTML in the script.php file and then would only need the ID of the DIV in which you will be displaying the result $("#madlibs")
You can use any server side language to achieve the same result, just swap out the external file call to the appropriate name and extension (.asp, .cfm, etc.)
Here is a link to the original tutorial that helped me with a similar project:
http://www.sitepoint.com/ajax-jquery/
I hope this helps. Sorry, but I couldn't come up with a purely Java of JavaScript solution on lunch.
I have the following Razor code:
#{
if (IsPost)
{
string pn=Request.Form["pn"];
if (!string.IsNullOrEmpty(pn)){
/* my code to call the stored procedure with pn as a parameter*/
}
}
}
And in the page, using JavaScript / HTML I need to tell the user about pn. Be it that it's empty, or report back that the value was inserted in the database.
How do I do that?
You need to insert the result into the rendering. There are a couple of ways of doing that. You could store it in a variable:
<script type="text/javascript">
var pn = '#(pn)';
alert('pn is ' + pn);
</script>
Or you could call a function, sort of like JSONP:
<script type="text/javascript">
parsePn('#(pn)');
<!-- ... extra logic ... -->
function parsePn(pn) {
alert('pn is ' + pn);
}
</script>
I've made a web site, and on my index page there are two links. One leads to /eng/index.htm and second to /hr/index.htm. I've also added a "Switch to" option on every /eng/ page and every /hr/ page to switch to other language.
I'm trying to make cookie that remembers users choice and next time he goes to index page it redirects him to the index page of the language he has chosen the first time.
And also if it's possible to change to cookie if users has clicked on "Switch to" link.
I've tried with many java scripts and suggestions from this site but no luck. :(
I've added this to my language index pages:
<script language="JavaScript" type="text/javascript">
var d = new Date()
var dMonth = d.getTime() + 30*24*60*60*1000
d.setTime(dMonth)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
function deleteCookie(){
var d = new Date(2000,1,1)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
}
</script>
And this to my index page:
<script language="JavaScript" type="text/javascript">
var c = document.cookie.split(';')
if(c.length >0){
for(m=0;m<c.length;m++){
if(c[m].indexOf('startPage')>-1){
location.replace(c[m].split('=')[1])
}
}
}
</script>
you need to add domain=example.com; too your cookie, because you will be setting it on the second page, but reading it on the index page. To make switch to thing work, just run your delete cookie function and create a new one with the corresponding url.
This should be simple - don't get what I'm doing wrong! This is a very basic test (I'm new to PERL and Javascript) - this is the CGI file:
#! /usr/local/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n" ;
print "<head>Hello\n";
print '<script type="text/javascript" language="javascript" src="wibble.js">\n';
print "</script>\n";
print "</head>\n";
print "<body>\n";
$fred = "Fred";
$numb = 7;
print <<TEST;
<p>Starting...</p>
<p><script type="text/javascript" language="javascript">
theText = "$fred";
theNum = "$numb";
document.writeln("Direct write...");
document.writeln("Number is: " + theNum);
document.writeln("Text is: " + theText);
testWrite(theNum, theText);
</script></p>
<p>...ending JS</p>
TEST
and in wibble.js:
function testWrite(num1, txt1)
{
document.writeln("In testWrite...");
document.writeln("Number is: " + num1);
document.writeln("Text is: " + txt1);
}
In my browser, I get the first set of writeln's but my function is never called. The error on the webpage says 'Object expected' at line 15 (the 'print <<TEST' line).
I mostly suspect I haven't got the right path in my src element but I've tried every combination I can think of ('.', './', full path etc) - nothing works. The js file is in the same dir as the CGI file.
(I actually originally had the function call with no parameters, hoping that theNum and theText are global and will still work (that was the original point of this test program)).
Please put me out of my misery...
As requested, here is source code from browser:
<html>
<head><script type="text/javascript" language="javascript" src="wibble.js"></script>
</head>
<body>
<p>Starting...</p>
<p><script type="text/javascript" language="javascript">
theText = "Fred";
theNum = "7";
document.writeln("Direct write...");
document.writeln("Number is: " + theNum);
document.writeln("Text is: " + theText);
testWrite(theNum, theText);
</script></p>
<p>...ending JS</p>
</body>
</html>
and this is the actual output on the web page:
Starting...
Direct write... Number is: 7 Text is: Fred
...ending JS
Did you check your server's log to see if wibble.js is ever requested? If it's not, then there's your problem. As well, while not really the problem, this line:
print "<head>Hello\n";
is generating bad html. You can't have "bare" text in the <head> block.
For global JS variables, you use the var keyword.
x = 7; // local
var y = 7; // global
I'm a newb when it comes to javascript. Perhaps somebody could help me with this. I assume it is not very complicated. This is what I would like:
<SCRIPT type=text/javascript>
var StandardURL = "http://site/Lists/test/AllItems.aspx";
</script>
<SCRIPT type=text/javascript>
var FilterURL = "http://site/Lists/test//AllItems.aspx?FilterField1=Judge&FilterValue1=";
</script>
var DynamicURL = FilterURL + DynamicUserInf (no space between it should be like one url link), dynamicuserinf contains different value depending on the user that is logged in no need to worry what is in it. It already contains a value befor this runs
var CurrentURL = current URL where this script is loading
<script language="JavaScript" type="text/javascript">
if (CurrentURL == StandardURL) { location.href= (DynamicURL);}
</script>
ElSE do nothing (i assume this is not neccarry with only one if statement)
Hopefully not much of a mess.
For getting current URL :
var CurrentURL = document.URL;
For joining/concatenating the variables (assuming that DynamicUserInf is a variable), you've already got it correct :)
var DynamicURL = FilterURL + DynamicUserInf;