PDA

View Full Version : Joomla Help with Javascript


scorpio4646
02-26-2009, 11:59 AM
Hi Guys
new to this and joomla
I have the following code which I want to use in Joomla. Can anyone tell me where to put it. (Dont be rude;))
Have tried several location but havent got it to run yet

Code
<script type="text/javascript">

//JK Popup Window Script (version 3.0)- By JavaScript Kit (http://www.javascriptkit.com)
//Visit JavaScriptKit.com for free JavaScripts
//This notice must stay intact for legal use
//Win Type: Pop Up | Session Only

//Specify URLs to randomly select from and popup/popunder:
//To display a single URL, just remove all but the first entry below:
var popurls=new Array()
popurls[0]="http://www.javascriptkit.com"
popurls[1]="http://www.dynamicdrive.com"
popurls[2]="http://www.webmasterpick.com"
popurls[3]="http://www.codingforums.com"

function openpopup(popurl){
var winpops=window.open(popurl,"","width=,height=,status")
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function loadornot(){
if (get_cookie('jkpopup')==''){
openpopup(popurls[Math.floor(Math.random()*(popurls.length))])
document.cookie="jkpopup=yes"
}
}

loadornot()
</script>

code

Your help would be much appreciated

scorpio4646
02-26-2009, 12:45 PM
forgot to mention I want the popup to open when someone opens site

scorpio4646
02-26-2009, 04:54 PM
got a bit further with this it now works when I click on the login button

Eldarrion
02-26-2009, 05:06 PM
The following should work on page load:


document.onload = loadornot;


Just stick it within your javascript and you're set.

scorpio4646
02-26-2009, 05:24 PM
Thnx for the reply

function loadornot(){
if (get_cookie('jkpopup')==''){
openpopup(popurls[Math.floor(Math.random()*(popurls.length))])
document.cookie="jkpopup=yes"


this is alraedy in there
I am just not sure where I should put the code this is specific to Joomla

Eldarrion
02-26-2009, 05:45 PM
Looking at your code, I don't see it. Let me try and translate what "document.onload = loadornot;" does: "When the document has finished loading call the function by the name loadornot". What I see in your code is the following:


function loadornot(){
if (get_cookie('jkpopup')==''){
openpopup(popurls[Math.floor(Math.random()*(popurls.length))])
document.cookie="jkpopup=yes"
}
}

loadornot()


Replace the bold red text with:

document.onload = loadornot;


Which in turn will cause the function loadornot to be executed upon completed loading of the webpage... and essentially achieve the effect you want, i.e. open a pop-up window with the random link when the webpage loads. As to where you should insert your entire JavaScript... that really is up to you, I'm guessing at present it is located in the <head> of the webpage that you want to have a pop-up appear at. If not, I'd suggest you move it there.

scorpio4646
02-26-2009, 06:05 PM
I will try your suggestion and let you know how I get on.

I tried to put it in the index.php file but it didnt like it , so I put it an avascript.js file. It doesnt load when I open page, but does open when I login as a user

Eldarrion
02-26-2009, 06:13 PM
Hmmm, it should work. Try the following in that case:


<script type="text/javascript" src="avascript.js"></script>
<script type="text/javascript">
document.onload = loadornot;
</script>


This should go into the <head> of your index.php file. Would help if you had a link to provide... or the error message you get if index.php produces an error when you try to add the code.

scorpio4646
02-26-2009, 06:50 PM
mmm got all sorts of errors when i did that about header cant be reloaded etc


Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/johnnyb/public_html/index.php:2) in /home/johnnyb/public_html/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/johnnyb/public_html/index.php:2) in /home/johnnyb/public_html/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/johnnyb/public_html/index.php:2) in /home/johnnyb/public_html/libraries/joomla/session/session.php on line 426




this is what happens
go to http://www.radioqfm.com/index.php

and nothing pops up, this is where I want it to pop up



so I dont think its a problem with the script but where I have placed it in Joomla

scorpio4646
02-26-2009, 07:03 PM
I just noticed

<script type="text/javascript" src="avascript.js"></script>


shouldnt that read

<script type="text/javascript" src="javascript.js"></script>

Eldarrion
02-26-2009, 07:10 PM
It's what you gave me as an information for the file itself (I personally thought you were giving me a filename, so I copied it literally).

I will try your suggestion and let you know how I get on.

I tried to put it in the index.php file but it didnt like it , so I put it an avascript.js file. It doesnt load when I open page, but does open when I login as a user

Just replace with the name of the file you put your script into. Also... and this is my bad... instead of using document.onload, use window.onload - which in turn should fix the problem itself.

scorpio4646
02-26-2009, 07:44 PM
This should go into the <head> of your index.php file. Would help if you had a link to provide... or the error message you get if index.php produces an error when you try to add the code.problem is there isnt a <head> in my index.php
its just php script

scorpio4646
02-26-2009, 07:58 PM
sorted it now thanks I found an index.php in the template folder , put the whole script in there now it work fine

Thnx for your time and help