dadamssg Posted November 16, 2010 Share Posted November 16, 2010 Ok..this is kind of complicated. I wrote a php script that spits out a calendar in javascript. I use it as the source of a javascript function. heres the javascript code that will be placed in a html webpage <script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.php?m=11&y=2010"></script> as you can see the script uses the $_GET['m'] and $_GET['y'] to determine which month to display. Again it all works but i want to have the script produce Next and Previous month links. and then when clicked it would change the source of the script to have different m & y values. I kinda doubt this is possible but can anyone recommend a way to achieve this? Quote Link to comment Share on other sites More sharing options...
Adam Posted November 16, 2010 Share Posted November 16, 2010 The browser won't make another HTTP request to the script just because you change the src attribute of the script tag. This is something that should be contained within a function and bound to an event: <script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.js"></script> <script type="text/javascript"> window.onload = function() { document.getElementById('calendarPopButton').onclick = function() { popCalendar(this); } } </script> The external script would contain the 'popCalendar' function. Passing 'this' to it would pass the 'calendarPopButton' element, for uses in positioning and such. This way you have a re-usable function, that can be easily updated through other functions (i.e. 'setCalendarMonth()' or something). Having said all that; jQuery has a powerful, yet simple datepicker widget available within the UI library that would make this a thousand times easier for you. Quote Link to comment Share on other sites More sharing options...
Adam Posted November 16, 2010 Share Posted November 16, 2010 Just to clarify there actually, slapping all the code in a function won't magically work. I was just saying how something like this *should* be written, as what you're currently trying to do makes no sense. Quote Link to comment Share on other sites More sharing options...
dadamssg Posted November 17, 2010 Author Share Posted November 17, 2010 hey thanks...i figured out a way to do what i want. I have a javascript that has a php file as its source to spit out javascript. Then i have a function in the head that updates the div with a php script that spits out html. <html> <head> <script src="http://code.jquery.com/jquery-1.4.4.js"></script> <style type="text/css"> body{ background: url(http://localhost:8888/css/lightgreenstripe.png); } </style> <script type="text/javascript"> function ChangeMonth(X,Y){ var changemonthlink = "http://localhost:8888/calendars/calendar_html.php?m=" + X + "&y=" + Y; $("#relEVENTz").load(changemonthlink); }; </script> <link type="text/css" href="http://localhost:8888/css/dynamic.php?color=blue" rel="stylesheet" /> </head> <body> <br> <div id="relEVENTz"> <script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.php?m=11&y=2010"></script> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 Well, to answer the question originally posed - it is absolutely possible to change the source of an included javascript source file. Done this before. You just use javascript to change the src attribute for the script object. The easiest way to do this is to include an ID in the script tag. Here are a few test files to illustrate how it works. html document: <html> <head> <script id="jsInclude" type="text/javascript" src="one.js"></script> <script type="text/javascript"> function changeJS(jsFile) { document.getElementById('jsInclude').src = jsFile; } </script> </head> <body> <button onclick="changeJS('one.js');">Use JS1 Include</button> <button onclick="changeJS('two.js');">Use JS2 Include</button> <br /><br /> <button onclick="test();">Run the test function</button> </body> </html> JS source file one.js function test() { alert('This is test in include 1.'); } JS source file two.js function test() { alert('This is test in include 2.'); } Quote Link to comment Share on other sites More sharing options...
Adam Posted November 17, 2010 Share Posted November 17, 2010 mjdamato I think you'll find that doesn't work in FF, Chrome or Safari. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 mjdamato I think you'll find that doesn't work in FF, Chrome or Safari. No, I won't find that. I only have IE on my work PC and I refuse to test it at home on the basis that I might have to admit there is a problem with the code I submitted. I reject your reality and substitute my own. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.