Jump to content


Photo

onload event for other than body element


Best Answer ankur0101, 10 March 2013 - 05:00 AM

Problem solved, I left --> }); after writing myFunc()

Things are working as I wanted.

 

Thanks for your help trq regarding jquery ready. I am bad at java script n jquery.

Go to the full post


  • Please log in to reply
10 replies to this topic

#1 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 07 March 2013 - 06:36 AM

Hi,
I have following code >
<select name="type">
             <option value="all">All</option>
             <option value="act" selected="">Active</option>
             <option value="res">Re-signed</option>
             </select>
Is there any way I can add onload event to <select> ?
onload is only available for <body>. I am using MVC architecture in PHP. My <body> is located in header.php so I cannot use onload in <body>.
What I want to have is when a page loads, my java script function will automatically execute. It would be fine if I put event in any other tag such as <p>


#2 trq

trq

    Advanced Member

  • Administrators
  • 30,995 posts
  • LocationSydney, Australia.

Posted 07 March 2013 - 06:54 AM

Most JavaScript frameworks have this (or similar) functionality built in.

For instance, jQuery triggers an event when the DOM is ready. http://api.jquery.com/ready.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 09 March 2013 - 09:21 AM

Hey trq, how can I execute any existing java script function ? Do I have  to write like this >

 

 

<script>
$(document).ready(function () {
myFunc();
});
</script>


#4 trq

trq

    Advanced Member

  • Administrators
  • 30,995 posts
  • LocationSydney, Australia.

Posted 09 March 2013 - 04:59 PM

Yes.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 10 March 2013 - 12:10 AM

Does not work.

My function is myFunc(str) and I write as follows :

 

 

<script>
$(document).ready(function () {
myFunc();
});
</script>
 
This didnt work so I added parameter i.e. str
<script>
$(document).ready(function () {
myFunc(str);
});
</script>
 
Here is how my page looks like
 
 
<script>

    	function myFunc(str) {
		 ...
                 ...
</script>

<script>
	$(document).ready(function () {
	myFunc(str);
	});
</script>

It does not work at all



#6 trq

trq

    Advanced Member

  • Administrators
  • 30,995 posts
  • LocationSydney, Australia.

Posted 10 March 2013 - 12:12 AM

You are of course including the jQuery framework somewhere?

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#7 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 10 March 2013 - 12:34 AM

I found out no jquery is running at all. I have added jquery file. in header.



#8 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 10 March 2013 - 12:36 AM

I tried to do

 

 

$(windows).load(function () {
alert('hi);
myFunc(str);
});
 
and 
 
document.onload=function{
alert('hi');
};
 
 
I know this is simple thing but god knows its not working

Edited by ankur0101, 10 March 2013 - 12:37 AM.


#9 trq

trq

    Advanced Member

  • Administrators
  • 30,995 posts
  • LocationSydney, Australia.

Posted 10 March 2013 - 01:30 AM

Post your actual code.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#10 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 10 March 2013 - 04:49 AM

I came to know why it is not working. As I said earlier, I am using MVC (Codeigniter here)

 

In Header.php,

I am loading calling css and js files along jquery main file.

 

In mainbody.php, 

I am writing myFunc() and calling that read jquery code.

 

This is why it is not working. I wrote .ready() thing that you said in header.php and it worked fine.

 

Now problem is I dont want to write that in header.php because it is common to all pages. And myFunc() is only called on maonbody.php.This is why I want to write jquery outside <head> i.e. in <body>

If jquery does not work, how can I do with java script ?



#11 ankur0101

ankur0101

    Advanced Member

  • Members
  • PipPipPip
  • 243 posts
  • LocationMumbai, India

Posted 10 March 2013 - 05:00 AM   Best Answer

Problem solved, I left --> }); after writing myFunc()

Things are working as I wanted.

 

Thanks for your help trq regarding jquery ready. I am bad at java script n jquery.


Edited by ankur0101, 10 March 2013 - 05:02 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com