LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2011, 05:21 PM   #1
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Rep: Reputation: 53
javascript ignores format modifying instruction


Hi. All I want to do is fade some text in and out via the opacity attribute, and change the text when it hits zero. My script changes the text on time, but ignores the formatting effects. I'm not worried at this stage about the else statement in the startFadeEffect() function. I just want the fade effect to work here the way it should. You guys are always helpful, so thanks in advance.

Code:
<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>Test June 22, 2011</title>

<style type="text/css">

#covenant {
	position:relative; 
	left:5px; 
	text-align:center;
	font-weight:bold;
	font-size:24px;	
	}

</style>

<script type="text/javascript">

var thisOpacity;
var thisString="I will speak what is noble, not base.";
var adjustBy=(-5);
var browzer=navigator.appName;


function startFadeEffect()
	{
	var thisRegEx=/Microsoft/;

	var result=browzer.search(thisRegEx);

	if (result!=-1)
		{
		thisOpacity=100;
		setTimeout("fadeEffect1()",1500);	//Microsoft browser
		}
		else
			{
			thisOpacity=1.0;
			setTimeout("fadeEffect2()",1500);	//non-Microsoft browser
			}

	}

function fadeEffect1()
	{
	thisOpacity+=adjustBy;

	document.getElementById('covenant').style.filter="alpha(opacity="+thisOpacity+")";
		
	if (thisOpacity==0)
		{
		switchPledge();
		adjustBy=5;
		}

	if (thisOpacity==100)
		{
		adjustBy=(-5);
		}


	setTimeout("fadeEffect1()",75);
	}

	
	

function switchPledge()
	{
	if (thisString=="I will speak what is noble, not base.")
			{
			thisString="I will build up, and not tear down.";
			}
			else if (thisString=="I will build up, and not tear down.")
				{
				thisString="I will be kind, and not destroy.";
				}
				else if (thisString=="I will be kind, and not destroy.")
					{
					thisString="I will speak what is noble, not base.";
					}
	document.getElementById('covenant').innerHTML=thisString;
	}

</script>

</head>

<body onload="startFadeEffect()">


<div id="covenant">
I will speak what is noble, not base.
</div>


</body>

</html>

Last edited by bluegospel; 06-24-2011 at 09:31 PM. Reason: correct confused content
 
Old 06-27-2011, 11:15 AM   #2
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Am I being ignored because none can help, or because I've overlooked something grossly obvious?
 
Old 06-27-2011, 11:19 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Why aren't you using jQuery? The effect you want is part of its standard library.

http://api.jquery.com/fadeOut/

Last edited by dugan; 06-27-2011 at 11:25 AM.
 
Old 06-27-2011, 11:32 AM   #4
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Why aren't you using jQuery? What you want is part of its standard library.
Okay, I'll try that. Thanks.

For the most part, all this is new to me. So when I hear things like jQuery in the midst of other jargon, I often can't help but overlook it, if I'm to accomplish what I want before I reach 105 years of age. Most of you guys (even those calling themselves newbies) are somewhat well versed in technical computing.

Also, I often like to try this sort of thing, even though I may be reinventing a duck.

Thanks again!
 
Old 06-27-2011, 11:36 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
I should also point out that one of the reasons Javascript frameworks exist is to handle portability issues between browsers. With jQuery, therefore, you will not need to test whether the user is running Internet Explorer. You just write code and it will work in both Firefox and IE.
 
Old 06-27-2011, 11:39 AM   #6
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
Why aren't you using jQuery? What you want is part of its standard library.
Does this library load in the user's browser when the script runs?
 
Old 06-27-2011, 11:50 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
You load it with a script tag. "script src="

More information is on its homepage, on huge numbers of other sites (hint: search stackoverflow.com for "jquery tutorial video"), and in bookstores ("jQuery In Action" is a recommended book).

Code:
// I have NOT tested this. But it would be something like this...

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
    $(function () {
        var fadeInOut = function () {
            switchPledge();
            $(this).fadeOut(75, fadeInOut);
        };
        $('covenant').fadeOut(1500, fadeInOut)
    });
</script>

Last edited by dugan; 06-27-2011 at 11:59 AM.
 
Old 06-27-2011, 12:01 PM   #8
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
With jQuery, therefore, you will not need to test whether the user is running Internet Explorer.
Is that just with items in the jQuery Library? What if I want to use javascript features that aren't included in that library, and behave differently across browsers? Will I then need to test even though I include the library?
 
Old 06-27-2011, 12:04 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
Originally Posted by bluegospel View Post
Is that just with items in the jQuery Library? What if I want to use javascript features that aren't included in that library, and behave differently across browsers? Will I then need to test even though I include the library?
That is correct.

However, you'll have to be more specific about what you mean by "items that aren't included in that library." DOM manipulation and AJAX functionality are both very complete, for example. The only things that aren't included are the new features in HTML 5 (e.g. controlling embedded audio or drawing on the canvas).
 
Old 06-27-2011, 12:04 PM   #10
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
What's this? I thought I could include it just by:

Quote:
<script type="text/javascript" src="jquery.js"></script>
 
Old 06-27-2011, 12:06 PM   #11
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
Originally Posted by bluegospel View Post
What's this? I thought I could include it just by:

<script type="text/javascript" src="jquery.js"></script>
Of course you can. However, loading it from Google's content delivery network saves you from having to download it and load a local copy.

Also, you don't need the "type=text/javascript" attribute if you're using the HTML 5 doctype (which you are).

Last edited by dugan; 06-27-2011 at 12:09 PM.
 
Old 06-27-2011, 12:12 PM   #12
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
However, you'll have to be more specific about what you mean by "items that aren't included in that library."
I mainly just mean, when the interface they use for a function doesn't quite meet my needs, or when I do, for whatever reason, want to reinvent a duck.

Quote:
e.g. controlling embedded audio
Eventually, I'll need that.
 
Old 06-27-2011, 12:18 PM   #13
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Quote:
loading it from Google's content delivery network saves you from having to download it and load a local copy
Is it more likely that google will maintain that file in that location, publically, indefinitely for the future, or my web host (Arvixe)? I ask to spare myself from having to change the src in the future.
 
Old 06-27-2011, 12:37 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Anyway, here's the jQuery version (tested, this time):

Code:
<!DOCTYPE html>

<html>

	<head>

		<meta charset="utf-8" />

		<title>Test June 22, 2011</title>

		<style type="text/css">

			#covenant {
				position:relative; 
				left:5px; 
				text-align:center;
				font-weight:bold;
				font-size:24px;	
			}

		</style>

	</head>
	<body>
		<div id="covenant">
			I will speak what is noble, not base.
		</div>
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
		<script>
			$(function () {
				var switchPledge = function () {
					var pledge = $(this).text();

					if (pledge === 'I will speak what is noble, not base.') {
						$(this).text('I will build up, and not tear down.');
					}
					else if (pledge === 'I will build up, and not tear down.') {
						$(this).text('I will be kind, and not destroy.');
					}
					else {
						$(this).text('I will speak what is noble, not base.');
					}
					$(this).show();
					$(this).fadeOut(1500, switchPledge);
				};

				$('#covenant').fadeOut(1500, switchPledge);
			});
		</script>
	</body>
</html>
Quote:
Is it more likely that google will maintain that file in that location, publically, indefinitely for the future, or my web host (Arvixe)? I ask to spare myself from having to change the src in the future.
As mentioned, you have both options. So you'll have to decide that for yourself.

Quote:
I mainly just mean, when the interface they use for a function doesn't quite meet my needs, or when I do, for whatever reason, want to reinvent a duck.
When that comes up, extending jQuery is another option. The way to do so is very well documented, and it's as easy as writing your own functions.

Last edited by dugan; 06-27-2011 at 12:43 PM.
 
Old 06-27-2011, 12:47 PM   #15
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
Okay, thanks for all your help Dugan. Now to study up on jQuery (so many books, so little time).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help modifying Firefox custombutton javascript David the H. Programming 0 08-21-2010 11:14 AM
dvgrab ignores -format option? fukawi2 Linux - Desktop 0 08-02-2010 07:50 PM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Which linux instruction is equivalent to windows' instruction "tracert"? backpacker Linux - Software 1 04-04-2006 10:55 PM
Modifying Invoice format in GNUCASH cvc505 Linux - Software 0 10-03-2004 03:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration