Redirecting based on user language choice - javascript

Can someone tell me why this code is not working.
I have and index page where you choose your language using 2 links which lead you to language index pages.
I've added this code to my index page:
<script type="text/javascript">
function get_cookie (username)
{
var results = document.cookie.match(username + '=(.*?)(;|$)');
if (results[1] == "en")
location.replace("http://x.com");
if (results[1] == "es")
location.replace("http://y.com");
else
return null;
}
get_cookie('username');
</script>
And this code to my language index pages:
<script type="text/javascript">
var cookieDate = new Date(2012, 8, 08)
document.cookie = "username=en;expires=" + cookieDate.toGMTString();
</script>
The weird thing is that when I open index page on my desktop it redirects me, but when I open it on web it doesn't.

Related

extract specific data from a HTML table or hide specific lines

Please help as this is doing my head in. My scripting skills are basic but I learn quickly.
I have HTML code that runs a java script on an external website and displays a table.
In my case it is a football leagues upcoming fixtures. I want to display this table using the code on our clubs website but I only want it to show certain lines of the table, ie only the matches that include the 2 teams from our club which is Watford Ladies FC.
Is there a way to either modify this script to hide certain lines or another script to extract certain lines and populate into another table?
The code I have to work with is:
<div id="lrep509554198" style="width: 350px;">Data loading....click here for Under 15's Division Two<br/><br/>FULL-TIME Home</div>
<script language="javascript" type="text/javascript">
var lrcode = '509554198'
</script>
<script language="Javascript" type="text/javascript" src="http://full-time.thefa.com/client/api/cs1.js"></script>
Unfortunately it requires you to allow unsafe script so it wont generate properly here.
If you copy the code into a HTML file and load into IE, allow scripts, youll see the table.
Many Thanks
Mark
Try adding this script to your html,
<div id="lrep509554198" style="width: 350px;">Data loading....click here for Under 15's Division Two<br/><br/>FULL-TIME Home</div>
<script language="javascript" type="text/javascript">
var lrcode = '509554198'
</script>
<script language="Javascript" type="text/javascript" src="http://full-time.thefa.com/client/api/cs1.js"></script>
<script type="text/javascript">
function load() {
var trElements = document.getElementsByTagName('tr');
var trsWithMatch = new Array();
for (i = 0; i < trElements.length; i++) {
var innerChildr = trElements[i].innerHTML;
if(innerChildr.indexOf('Watford Ladies Wasps') == -1) {
trsWithMatch.push(i);
}
}
if(trsWithMatch.length != 0) {
for (i = 0; i < trsWithMatch.length; i++) {
var indexFor = trsWithMatch[i];
var trMatched = trElements[indexFor];
trMatched.style.display = "none";
}
}
}
window.onload = load;
</script>

how to using javascript function in smarty tpl

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>

Redirecting using cookies

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.

Load script only for returning USER - once!

I need a script which will load another script only for returning user!
The script which will need to load this script
<a href="http://www.w3.org/" >W3C</a>
I have a script which loads a popup only for new customers:
<SCRIPT LANGUAGE="JavaScript">
<!-- Script courtesy of http://www.web-source.net - Your Guide to Professional Web Site Design and Development
function GetCookie(name) {
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i,j)==arg)
return "here";
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
}
return null;
}
var visit=GetCookie("COOKIE1");
if (visit==null){
var expire=new Date();
window.name = "thiswin";
newwin=open("yourpagename.html", "dispwin",
"width=450,height=455,scrollbars=yes,menubar=no");
expire=new Date(expire.getTime()+7776000000);
document.cookie="COOKIE1=here; expires="+expire;
}
// -->
</SCRIPT>
Yet I need the script only to load once for the returning customers every 1 day
thanks again
7776000000 equals 90 days.
Make the cookie expire every 24 hours [86400000] and you get once a day.

Little help with some (simple) Javascript code

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;

Categories

Resources