]]> FCKeditor: Resolve “FCKeditorAPI is not defined?issuehttp://www.aygfsteel.com/sealyu/archive/2010/06/22/324103.htmlsealsealMon, 21 Jun 2010 16:12:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/22/324103.htmlhttp://www.aygfsteel.com/sealyu/comments/324103.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/22/324103.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/324103.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/324103.htmlFCKeditor offers a complete JavaScript API so you can interact with
it once the editor is loaded and running.
Once loaded, the editor registers a global object called FCKeditorAPI.
This object offers the entry point to interact with any editor instance
placed in a page.
NOTE: The FCKeditorAPI object will
not be available during the page load. You need to wait for the editor
to be loaded to use it.
Often, I see that people complain about a JavaScript error,
“FCKeditorAPI is not defined”, when they try to use FCKeditorAPI.
For example
The following code will throw a JavaScript error when you try to use
the FCKeditorAPI -
You can resolve the JavaScript error by adding a line of code (Line #
4) as shown below -
]]>IE和Safari中重命名html元素需要注意的问题http://www.aygfsteel.com/sealyu/archive/2010/06/11/323288.htmlsealsealThu, 10 Jun 2010 16:10:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/11/323288.htmlhttp://www.aygfsteel.com/sealyu/comments/323288.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/11/323288.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/323288.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/323288.html
q两天在处理览器兼容的时候碰到很多问题,特别是在重新命名checkbox的时候,在IE和Safari中都到一些怪异的问题?br />
IE中不能在q行旉命名元素?/li>
按照微Y的解释,q行时是无法重命名的Q?br />
After some digging, I found an
explanation in the MSDN
DHTML
reference, on the page describing the NAME
Attribute.
The NAME attribute cannot be set at run time on elements
dynamically created with the createElement method. To create an element
with a name attribute, include the attribute and value when using the
createElement method.
el = (typeof el ==="string") ? document.getElementById(el) : el;
el.name = newName;
if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { // Internet Explorer test
el.mergeAttributes(document.createElement("<INPUT name='"+ newName +"'/>"), false);
var checkBoxes = document.getElementsByName("testCheckbox"); var checkBoxesSize = checkBoxes.length; for (var i = checkBoxesSize -1; i >=0; i--) { if (checkBoxes[i] &&!checkBoxes[i].checked) {
setElementName(checkBoxes[i],"testCheckbox-new");
}
}
]]>Detecting Internet Explorer More Effectivelyhttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323114.htmlsealsealTue, 08 Jun 2010 20:35:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323114.htmlhttp://www.aygfsteel.com/sealyu/comments/323114.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323114.html#Feedback2http://www.aygfsteel.com/sealyu/comments/commentRss/323114.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/323114.htmlThis
topic demonstrates how to detect current and future versions of Windows
Internet Explorer more effectively.
Many Web designers use browser
detection techniques to ensure that their sites display properly when
viewed with specific browsers. Some browser detection techniques
encounter problems when viewed with later versions of the browser
they're tailored for. For example, many Web designers used Cascading
Style Sheets (CSS) rules that relied on the implementation of CSS in
Microsoft Internet Explorer 6 to detect that version of the browser.
When Internet Explorer 7 provided additional support for the CSS, Level 2
(CSS2) standard, many of these rules (also known as "CSS hacks") failed
to detect the new version of the browser. As a result, sites that
relied on these rules no longer displayed as intended. When using
browser detection techniques, use techniques that support current and
future versions of the browser you're targeting.
This topic
examines four different ways to detect Internet Explorer and also
provides a brief look at alternatives to browser detection. Advantages
of each technique are reviewed and suggestions are offered for effective
use. In addition, examples demonstrate techniques that support current
and future versions of Internet Explorer.
Parsing
the User-Agent String
The most common way to detect Internet
Explorer is to use client-side scripting to parse the user-agent
string and extract the version number from the version token. The
following example shows the preferred way to do this with JavaScript.
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[".0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
As you review this example, please note
the following:
A regular expression extracts the version
number from the user-agent version token. Regular expressions let you
specify optional conditions to match, so that you can match a larger
number of version tokens, not just those that match a strict set of
conditions.
The version number extracted from the version
token is formally converted to a numeric value. Care must be taken
because pre-release versions of Internet Explorer typically add letters
to the version token. For example, the version token for pre-release
versions of Internet Explorer 7 was "MSIE 7.0b."
The
checkVersion() function verifies that the browser is version 8.0 or
later. This ensures that the welcome message is displayed for those
using newer versions of the browser.
This example
properly detects most versions of Internet Explorer, but only if
scripting is enabled. However, the user-agent string is dynamic; it can
be changed by the end user, by browser extensions, and by operating
system updates. As a result, there's no guarantee that the user-agent
string accurately reflects the browser being used. The next sections
show techniques that may be more effective alternatives.
Note It is not recommended that you block access to content
based on the user-agent string of the browser. If you do have to offer
different content to different versions of the browser due to improved
capabilities, you want to ensure that future versions of the browser are
not blocked. Serving content based solely on the user-agent string is
often an unreliable way to detect the full capabilities of the browser.
For
more information about the user-agent string and Internet Explorer's
user-agent tokens, see Understanding
User-Agent Strings.
Using Conditional
Comments
If you are specifically interested in Internet Explorer,
conditional comments might be a more appropriate choice. The following
example shows an effective way to use conditional comments to display
custom content.
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->
<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->
<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>
Like the earlier JavaScript example,
this example makes sure the current version is greater than or equal to a
specified version number. This ensures that the content designed for
the current version of the browser is properly displayed in a future
version.
This example carefully combines downlevel-revealed and
downlevel-hidden conditional comments to ensure that each message
appears only for the intended browsers.
Because conditional
comments do not rely on JavaScript, they are effective even if the user
has disabled scripting. Unlike the user-agent string, conditional
comments are typically updated when the browser is upgraded. This makes
them more reliable than user-agent strings. However, because no other
browser currently supports conditional comments, they are limited to
Internet Explorer. Depending on your needs, this may be an advantage or a
disadvantage.
For many Web sites, detecting a browser's features is
the preferred form of browser detection because it allow the Web
developer to focus on the general capabilities of a browser, rather than
the specific details of each browser release. The following example
demonstrates a simple form of feature detection.
Feature detection tends to support a
broader range of browsers. In addition, should a browser implement the
feature you're interested in, your content will be delivered without
requiring changes to your browser detection technique. As a result,
techniques that focus on features tend to require less maintenance than
techniques that rely on environmental factors, such as the user-agent
string. In addition, they tend to work across a wider variety of browser
devices, including screen readers, mobile devices, and so on.
Use
care when creating or using browser detection techniques that attempt
to detect a browser based on unsupported functionality. If a future
version of the browser implements the missing feature, it's entirely
possible your site may not display as intended.
Using
the ASP.NET HttpBrowserCapabilities Object
ASP.NET developers
can use the HttpBrowserCapabilities object to detect and respond
to almost any browser. In addition, alternate content is not sent to
the end-user's browser. The following example shows how to detect
Internet Explorer from an ASP.NET application using C#.
msg = "You're using a recent version of Internet Explorer.";
else
msg = "You should upgrade your copy of Internet Explorer.";
}
else
msg = "You're not using Internet Explorer.";
Label1.Text = msg;
}
Because the HttpBrowserCapabilities
object parses the user-agent string for you, you can use the MajorVersion
and MinorVersion
properties to determine the current version of the browser. Unlike the
JavaScript examples, these properties are numeric; MajorVersion
returns an integer value, and MinorVersion returns a
double value. Depending on the language used to implement your ASP.NET
application, however, you might still have to typecast these property
values to the data type you're using. To illustrate, the C# example
converts MajorValue to the floating point value
returned by the function.
If you use browser detection because
Internet Explorer doesn't appear to support a particular feature, an
alternate approach may be more effective. Before implementing new (or
revising existing) browser detection, it may be worthwhile to consider
one of the following approaches to solve the underlying problem.
Internet
Explorer allows Web developers to choose between various document
compatibility modes that affect the way the browser interprets and
displays content. If you are not seeing the results you expect, a
different document compatibility mode may provide the results you seek.
For more information, see Defining
Document Compatibility.
You may want to consider using the Developer
Tools to preview your pages in different document compatibility
modes or to experiment with different ways to achieve various visual and
layout effects. It's entirely possible there's another way to
accomplish the same result.
If the Document Object Model
(DOM) doesn't support a feature that you need, you may be able to add
the feature using DOM mutable prototypes.
If none of the
previous suggestions apply, it may be worthwhile to consider using an
alternate approach.
Summary
If
you must detect the browsers that view your Web sites, follow effective
practices: plan for future browser releases, convert values
appropriately, and design techniques to fail gracefully. Doing so will
reduce the long-term maintenance of your site and help ensure that your
site functions properly when viewed with newer versions of Internet
Explorer and other browsers.
Community Content
Bad browser detection
Using "v to detect IE is a bad idea because this will not
work if future versions of IE change this behavior. The officially
supported detection methods should be used.
However this example
from the article is misleading. Since other browsers don't support
conditional comments [if !IE] results in comments that will show up in
no browsers.
<![if !IE]>
<p>You're not using Internet
Explorer.</p>
<![endif]>
]]>Using the navigator object to detect client's browserhttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323113.htmlsealsealTue, 08 Jun 2010 20:34:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323113.htmlhttp://www.aygfsteel.com/sealyu/comments/323113.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/09/323113.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/323113.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/323113.html
Until one browser remains standing on the web (if ever), browser
detection
will continue to be part of any good JavaScripter's life. Whether
you're
gliding a div across the screen or creating an image rollover, it's
fundamental that only relevant browsers pick up on your code. In
this
tutorial we'll probe the navigator object of JavaScript, and show
how to use
it to perform browser detection, whether the subject is Firefox, Internet
Explorer 7, Opera, etc.
The navigator object
The navigator object was conceived back in the
days when
Netscape Navigator reined supreme. These days it serves as much as
an
irony of NS's diminished market share as way of probing browser
information.
The navigator object of JavaScript contains the
following
core properties:
Properties
Description
appCodeName
The code name of the browser.
appName
The name of the browser (ie: Microsoft
Internet Explorer).
appVersion
Version information for the browser
(ie: 4.75 [en] (Win98; U)).
cookieEnabled
Boolean that indicates whether the browser
has
cookies enabled.
language
Returns the default language of the
browser version (ie: en-US). NS and Firefox
only.
mimeTypes[]
An array of all MIME types supported
by the client. NS and Firefox only.
platform[]
The platform of the client's computer
(ie: Win32).
plugins
An array of all plug-ins currently
installed on the client. NS and Firefox only.
systemLanguage
Returns the default language of the operating system
(ie: en-us). IE only.
userAgent
String passed by browser as user-agent
header. (ie: Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1))
userLanguage
Returns the preferred language setting of
the user
(ie: en-ca). IE only.
Let's see exactly what these properties reveal of
the
browser you're currently using:
appCodeName: Mozilla appName: Netscape appVersion:
5.0 (Windows; zh-CN) userAgent: Mozilla/5.0 (Windows; U;
Windows NT 6.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 platform:
Win32
At a
glance
At a glance at the above table, you may be swayed
towards
turning to the following two properties to do your browser detection
bidding:
navigator.appName
navigator.appVersion
After all, you are trying to detect a browser's
name and
version right? However, they both will most likely mislead you. In
browsers such as various versions of Netscape and Firefox, these two
properties return simply "Netscape" for appName, and 5.0
for appVersion without any further distinction for
Firefox
and its version, and hence are pretty much useless in the real world.
For example, in both Firefox 1.x and Firefox 2.x, these two properties
return:
appName: Netscape
appVersion: 5.0 (Windows; en-US)
We need to turn to a property that's more thorough
in its
investigative work if we want more consistency and accuracy, and that
turns out to be navigator.userAgent.
Detecting Firefox x.x
In Firefox 2.0.0.13 for example,
the userAgent property reads:
UserAgent: Mozilla/5.0
(Windows;
U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
The detail we're interested in apparently lies at
the very
end, or Firefox/2.0.0.13. Different versions of Firefox
will
contain a different version number, but the pattern is consistent
enough.
The part we're interested in occurs after the string "Firefox/",
or the exact version number. There are many ways to get to it using
either
standard String
or RegExp
methods- I'm opting for the later here:
<script type="text/javascript">
if (/Firefox["/"s]("d+"."d+)/.test(navigator.userAgent)){ //test for
Firefox/x.x or Firefox x.x (ignoring remaining digits);
var ffversion=new Number(RegExp.$1) // capture x.x portion and store
as
a number
if (ffversion>=3)
document.write("You're using FF 3.x or above")
else if (ffversion>=2)
document.write("You're using FF 2.x")
else if (ffversion>=1)
document.write("You're using FF 1.x")
}
else
document.write("n/a")
</script>
Output:
You're using FF 3.x or above
Basically, I'm capturing just the versonMajor.versionMinor
portion of the full version number of Firefox (ie: 2.0.0.13 becomes
simply 2.0), and using that as basis to detect the various versions of
Firefox. Delving any deeper, and the returned version may no longer be
a
number but a string (ie: 2.0.0), which makes numeric comparisons
cumbersome.
Detecting IE x.x
In IE 7.0 for example,
the userAgent property reads:
UserAgent: Mozilla/4.0
(compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)
So the part we're interested in lies in the
middle, or
MSIE 7.0;. If you try a shortcut and use parseFloat
on the entire string to get to the 7.0 portion, it won't
work. This
is due to the way parseFloat works- by returning the
first number it
encounters, which in this case is 4.0. Once again we need to use
either
standard
String or
RegExp methods again to get to the actual version number; below
I'm
using RegExp as well:
<script type="text/javascript">
if (/MSIE ("d+"."d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (ieversion>=8)
document.write("You're using IE8 or above")
else if (ieversion>=7)
document.write("You're using IE7.x")
else if (ieversion>=6)
document.write("You're using IE6.x")
else if (ieversion>=5)
document.write("You're using IE5.x")
}
else
document.write("n/a")
</script>
Output:
n/a
Detecting Opera x.x
Detecting Opera using the navigator
object at
first appears to be tricky business due to the browser's identity
crisis. You see, Opera 8 and below by default identifies itself as IE6
(or lower) in the navigator object. Users can override
this
setting under "Edit Site Settings" in the toolbar to identify as
Opera or even another browser instead. Starting in Opera 9, the
browser
regains its confidence and identifies by default as itself, Opera,
though users can still modify this setting manually in the toolbar.
The
bottom line is, Opera can appear as either Opera, Internet Explorer,
or
another browser within a designated list in the navigator
object.
Lets take a
look at what navigator.userAgent in Opera 8.5 returns
depending on what it has chosen to identify itself as (whether
automatically or manually):
As IE6: Mozilla/4.0 (compatible; MSIE
6.0; Windows XP) Opera 8.5
[en] As Moz5: Mozilla/5.0 (Windows XP;
U) Opera 8.5 [en] As Opera: Opera/8.5 (Windows
XP; U) [en]
Notice how if it's set to identify as IE, MSIE 6.0
appears
within the string, while if set to identify as Mozilla,
Mozilla/5.0 appears instead. As Opera itself, Opera/8.5
appears. In all three cases, the one commonality that we can exploit
to
actually detect Opera and its true version regardless of which
identify
it's decided to take on is the string "Opera x.x" or "Opera/x.x"
within navigator.userAgent. In other words, there are two
versions of the target string we need to be aware of. With that said,
here's how you might go about testing for a specific version of Opera,
which turns out to be no different than the technique used for
detecting, say, Firefox:
<script
type="text/javascript">
//Note: userAgent in Opera9.24 WinXP returns: Opera/9.24 (Windows NT
5.1; U;
en)
// userAgent in Opera
8.5 (identified as IE) returns: Mozilla/4.0 (compatible; MSIE 6.0;
Windows
NT 5.1) Opera 8.50 [en]
// userAgent in Opera
8.5 (identified as Opera) returns: Opera/8.50 (Windows NT 5.1; U) [en]
if (/Opera["/"s]("d+"."d+)/.test(navigator.userAgent)){ //test for
Opera/x.x
or Opera x.x (ignoring remaining decimal places);
var oprversion=new Number(RegExp.$1) // capture x.x portion and store
as a
number
if (oprversion>=10)
document.write("You're using Opera 10.x or above")
else if (oprversion>=9)
document.write("You're using Opera 9.x")
else if (oprversion>=8)
document.write("You're using Opera 8.x")
else if (oprversion>=7)
document.write("You're using Opera 7.x")
else
document.write("n/a")
}
else
document.write("n/a")
</script>
Output:
n/a
Conclusion
We've seen how to use navigator to detect the
browser type
of your visitors. If the potential pitfalls and complexity of
usage of it
is a little too much for you, an alternative is to use
Object Detection. Whichever method you choose, just be
sure to
choose one!
]]>IE ?Firefox中取得createRange()的v点和l点的?/title>http://www.aygfsteel.com/sealyu/archive/2010/06/08/323099.htmlsealsealTue, 08 Jun 2010 14:23:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/08/323099.htmlhttp://www.aygfsteel.com/sealyu/comments/323099.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/08/323099.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/323099.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/323099.html
<head>
<title>TEST</title>
<style>
body,td{
font-family: verdana, arial, helvetica, sans-serif;
font-size: 12px;
}
</style>
<script type="text/javascript">
var start=0;
var end=0;
function add(){
var textBox = document.getElementById("ta");
var pre = textBox.value.substr(0, start);
var post = textBox.value.substr(end);
textBox.value = pre + document.getElementById("inputtext").value + post;
}
function savePos(textBox){
//如果是Firefox(1.5)的话Q方法很?br />
if(typeof(textBox.selectionStart) == "number"){
start = textBox.selectionStart;
end = textBox.selectionEnd;
}
//下面是IE(6.0)的方法,ȝ得很Q还要计上'"n'
else if(document.selection){
var range = document.selection.createRange();
if(range.parentElement().id == textBox.id){
// create a selection of the whole textarea
var range_all = document.body.createTextRange();
range_all.moveToElementText(textBox);
//两个rangeQ一个是已经选择的text(range)Q一个是整个textarea(range_all)
//range_all.compareEndPoints()比较两个端点Q如果range_all比range更往?further to the left)Q则 //q回于0的|则range_all往右移一点,直到两个range的start相同?br />
// calculate selection start point by moving beginning of range_all to beginning of range
for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
range_all.moveStart('character', 1);
// get number of line breaks from textarea start to selection start and add them to start
// 计算一?n
for (var i = 0; i <= start; i ++){
if (textBox.value.charAt(i) == '\n')
start++;
}
// create a selection of the whole textarea
var range_all = document.body.createTextRange();
range_all.moveToElementText(textBox);
// calculate selection end point by moving beginning of range_all to end of range
for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end ++)
range_all.moveStart('character', 1);
// get number of line breaks from textarea start to selection end and add them to end
for (var i = 0; i <= end; i ++){
if (textBox.value.charAt(i) == '\n')
end ++;
}
}
}
document.getElementById("start").value = start;
document.getElementById("end").value = end;
}
</script>
</head>
<body>
<form action="a.cgi">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>start: <input type="text" id="start" size="3"/></td>
<td>end: <input type="text" id="end" size="3"/></td>
</tr>
<tr>
<td colspan="2">
<textarea id="ta" onKeydown="savePos(this)"
onKeyup="savePos(this)"
onmousedown="savePos(this)"
onmouseup="savePos(this)"
onfocus="savePos(this)"
rows="14" cols="50"></textarea>
</td>
</tr>
<tr>
<td><input type="text" id="inputtext" /></td>
<td><input type="button" onClick="add()" value="Add Text"/></td>
</tr>
</table>
</form>
</body>
</html>
]]>Setting the name attribute in IE's DOMhttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322693.htmlsealsealThu, 03 Jun 2010 22:04:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322693.htmlhttp://www.aygfsteel.com/sealyu/comments/322693.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322693.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/322693.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/322693.htmlAn old problem with IE is that you can't set the name attribute on form
elements in the DOM directly. Fortunately there is a workaround to this issue
using IE's mergeAttributes
method. By creating a temporary named element acceptable by IE's createElement
method, you can merge the name attribute into the element you desire, allowing
you to name or rename an element. An example demo follows.
var setName = function(el, newName) {
el = (typeof el === "string") ? document.getElementById(el) : el;
el.name = newName;
if (/*@cc_on!@*/0) { // Internet Explorer test (needs to be modified for IE8)
]]>Inserting at the cursor using JavaScripthttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322689.htmlsealsealThu, 03 Jun 2010 19:12:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322689.htmlhttp://www.aygfsteel.com/sealyu/comments/322689.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/06/04/322689.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/322689.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/322689.htmlNOTE: I’ve released
a set of JavaScript Quicktags (that implement this technique) under
the LGPL license. Please look at this code as it has a variety of
enhancements and bug fixes.
I discovered a real JavaScript gem in PHPMyAdmin this
morning. They have code that inserts content into a <textarea> at
the cursor position instead of just at the beginning/end of the content.
This is something I thought wasn’t possible – I’m quite pleased I was
wrong.
I’ll be glad to see this code spread quickly to web forms that use
buttons to aid in styling text and inserting image and link tags. WordPress and other
blog and web forum packages would really benefit from this. (PHPMyAdmin
is GPL, so any other GPL app should be able to use their code directly
with attribution).
Here is a quick look at a generic version of the technique:
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos,
myField.value.length);
} else {
myField.value += myValue;
}
}
// calling the function
insertAtCursor(document.formName.fieldName, 'this value');
I guess when the mouse leaves the textarea, it still has a cursor
position left over. This code uses that position just as you’d want it
to.
果一个字W串中部包含该子字符串则indexOfq回returns "-1."
例子Q?br />
var the_word = "monkey";
//让我们从单词 "monkey"开始?nbsp;
var location_of_m = the_word.indexOf("m");
//location_of_m(字母m的位|?ؓ0Q因为字母m位于该字W串的v始位|?br />
var location_of_o = the_word.indexOf("o");
//location_of_o(字母o的位|?ؓ1?br />
var location_of_key = the_word.indexOf("key");
//location_of_key(key的位|?ؓ3因ؓ子字W串“key”以字母k开始,而k
在单词monkey中的位置??br />
var location_of_y = the_word.indexOf("y");
//location_of_y)字母y的位|???nbsp;
var cheeky = the_word.indexOf("q");
//cheeky值是-1Q因为在单词“monkey”中没有字母q?/p>
indexOf更实用之?
var the_email = prompt("What’s your email address?", "");
var the_at_is_at = the_email.indexOf("@");
if (the_at_is_at == -1)
{
alert("You loser, email addresses must
have @ signs in them.");
}
q段代码询问用户的电子邮件地址Q如果用戯入的电子邮g地址中不包含字符 ?nbsp;?/p>
C用PQ你输入的电子邮件地址无效Q电子邮件的地址必须包含字符@。"
3、charAt
chatAtҎ用于发现一个字W串中某个特定位|的字符?br />
q里是一个例子:
var the_word = "monkey";
var the_first_letter = the_word.charAt(0);
var the_second_letter = the_word.charAt(1);
var the_last_letter = the_word.charAt(the_word.length-1);
var the_url = prompt("What’s the URL?","");
var lead_slashes = the_url.indexOf("http://");
var domain_start = lead_slashes + 2;
var without_resource = the_url.substring(domain_start, the_url.length);
var next_slash = without_resource.indexOf("/");
var domain = without_resource.substring(0, next_slash);
基本的技巧是第Q个斜杠和第Q个斜杠之间的内容分d来:
var the_url = prompt("What’s the URL?","");
//q行代码向用戯问一个URLQ假讄戯入了
"http://www.webmonkey.com/javascript/index.html.";
var lead_slashes = the_url.indexOf("http://");
q行代码定W一个双斜杠的位|.在本例中lead_slashes的值是Q,因ؓ双斜杠的?/p>
for (loop=0; loop < friend_array.length;loop++)
{ document.writeln(friend_array[loop] + " is myfriend.<br>");}
q段代码字W串my_friends分割成包含5个元素的数组QJavaScript可以Z自动?/p>
立一个数l,所以你无需使用new Array()Q?/p>
字W串分割成数l之后,我们使用了@环语句写出每一个名Uͼ我们可以利用split?/p>
法简化前面所讲到的域名提取:
var the_url = prompt("What’s the URL?","");
var first_split = the_url.split("http://");
var without_resource = first_split[1];
var second_split = without_resource.split("/");
var domain = second_split[0];
function Plus(a, b)
{
var z = 0;
var i = 0;
for (i = 0; i < arguments.length; i++)
{
z += arguments[i];
}
setTimeout( function() {alert(z);}, 6000); //可以带变量参数的setTimeout调用形式
return z;
}
setTimeout( function(){ sum = Plus(x, y, z); }, 3000);/*除了可以带变量参数还可以获取q回值的setTimeout调用形式*/
]]>javascript CDATA的意?http://www.aygfsteel.com/sealyu/archive/2009/12/07/304961.htmlsealsealMon, 07 Dec 2009 02:04:00 GMThttp://www.aygfsteel.com/sealyu/archive/2009/12/07/304961.htmlhttp://www.aygfsteel.com/sealyu/comments/304961.htmlhttp://www.aygfsteel.com/sealyu/archive/2009/12/07/304961.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/304961.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/304961.htmlCDATA 内部的所有东襉K会被解析器忽略?/p>
假如文本中包含了大量?"<" ?"&" 字符 - 像~程代码中经常出现的情况一?- 那么q个 XML 元素可以被定义Z?CDATA 部分?/p>
CDATA 区段开始于 "<![CDATA["Q结束于 "]]>"Q?/p>
<script type="text/javascript">
<![CDATA[
function compare(a,b)
{
if (a < b)
{alert("a于b");}
else if (a>b)
{alert("a大于b");}
else
{alert("a{于b");}
}
]]>
</script>
<script type="text/javascript">
function compare(a,b)
{
if (a <b)
{alert("a于b");}
else if (a>b)
{alert("a大于b");}
else
{alert("a{于b");}
}
</script>
<script type="text/javascript">
<![CDATA[
function compare(a,b)
{
if (a < b)
{alert("a于b");}
else if (a>b)
{alert("a大于b");}
else
{alert("a{于b");}
}
]]>
</script>
<script type="text/javascript">
//<![CDATA[
function compare(a,b)
{
if (a < b)
{alert("a于b");}
else if (a>b)
{alert("a大于b");}
else
{alert("a{于b");}
}
//]]>
</script>
If value of the eval property is used in any way other than a direct call (that is, other than by the explicit use of its
name as an Identifier which is the MemberExpression in a CallExpression), or if the eval property is assigned to,
an EvalError exception may be thrown.
]]>在XHTML中用javascripthttp://www.aygfsteel.com/sealyu/archive/2009/05/13/270406.htmlsealsealWed, 13 May 2009 06:28:00 GMThttp://www.aygfsteel.com/sealyu/archive/2009/05/13/270406.htmlhttp://www.aygfsteel.com/sealyu/comments/270406.htmlhttp://www.aygfsteel.com/sealyu/archive/2009/05/13/270406.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/270406.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/270406.html
Making JavaScript Compatible with XHTML
XHTML is subject to the same syntactical rules as XML. Because of this, an
XHTML processor treats the characters < and & as markup, even if they reside inside a <script> block. Since the < and &
characters are also used by the JavaScript language, this creates a conflict. When an
XHTML processor sees these characters within the JavaScript code of a <script> block, it attempts to parse the JavaScript code as if it were markup, which causes the XHTML parser to fail.
You can get around this conflict and make all JavaScript code compatible with XHTML by placing the JavaScript code within a CDATA section. A CDATA section in XML/XHTML starts with the
characters <![CDATA[ and ends with the characters ]]>.
Any characters within the starting and ending element of a CDATA section are not treated by the XML/XHTML processor as markup, thus preventing a conflict.
Here is an example of how to declare JavaScript code within a CDATA section so that it is
compatible with XHTML:
<script type="text/javascript">
//<![CDATA[
alert("<This is compatible with XHTML>");
//]]>
</script>
Note that JavaScript source code must be placed within the opening and closing elements of the CDATA section. The CDATA section itself should be commented out with a JavaScript single-line comment // as in the example above. This is so that the JavaScript
interrupter does not interpret the CDATA markup as JavaScript, which would cause a JavaScript error.
JavaScript code that is imported into an XHTML document from an external source file is always
compatible with XHTML. So, for example, any code you place into the external file external.js and import into an
XHTML file via the src attribute of the <script> tag will be valid. Here is an example of how to import the file external.js:
The Pop-up Blocking feature blocks pop-up (and pop-under) windows initiated automatically by a Web site. Internet Explorer blocks Pop-up windows in the Internet and Restricted sites zones
by default. However, the Pop-up Blocker enables pop-up windows initiated by a user action. Users can configure Internet Explorer 6 for Windows XP with SP2 to be more or less restrictive. Users can also turn off the Pop-up Blocker altogether. Generally, the Pop-up Blocker enables a window to open under the following circumstances:
•
When initiated by user action, such as clicking a button or hyperlink
fool the browser into thinking that it was opened with a script
This opens a new page, (non-existent), into a target
frame/window, (_parent which of course is the window in
which the script is executed, so replacing itself),
and defines parameters such as window size etc, (in
this case none are defined as none are needed). Now
that the browser thinks a script opened a page we
can quickly close it in the standard way…