How do MSDN web pages access the clipboard?

  • Thread starter Thread starter Steve Richter
  • Start date Start date
S

Steve Richter

I am using the window.clipboardData.getData javascript method to copy
from the clipboard.

<SCRIPT LANGUAGE="JavaScript">
function ShowClipBoard()
{
var data = window.clipboardData.getData("text") ;
alert( data ) ;
}

</SCRIPT>

This works, but I am not prompted by the browser first, asking if I
want to allow access to the clipboard.

When I click "copy code" from an MSDN web page I first am prompted by
a browser message box asking if I want to allow access to the
clipboard.
http://msdn2.microsoft.com/en-us/library/ms533044.aspx

What method would the MSDN page be using to access the clipboard that
would cause the "allow access" prompt?

Is it just that I am running my test page from localhost? I want to
make sure I am using the most standards compliant method to copy to
and from the clipboard.

thanks,

-Steve
 
What method would the MSDN page be using to access the clipboard that
would cause the "allow access" prompt?

I don't actually know the answer, so what follows is just theory...

Firstly, the CopyCode function doesn't appear in the page's View Source, so
I'm guessing it's in some JavaScript include file...Therefore, it's
impossible to tell what it's doing without some serious hacking...

Secondly, this appears to be an IE-only thing - the "Copy Code" hyperlinks
don't appear at all when the page is viewed in any other browser, even the
very latest version of FireFox (2.0.0.4)...

Thirdly, I'm not actually certain that it's doing anything cleverer than
popping a showModalDialog and storing the return value in a JavaScript
variable... Simple enough to do but, of course, showModalDialog is not
cross-browser compatible and most certainly not standards-compliant...
Curiously enough, the "Copy Code" hyperlinks don't appear on Safari either,
even though showModalDialog *is* supported on that browser - draw your own
conclusions there... ;-)
I want to make sure I am using the most standards compliant method

In which case, you need to change your <script> tag to:

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

as the language tag is deprecated in XHTML...

Upper-case tag properties aren't supported either...
 
Is it just that I am running my test page from localhost?

yes, it asked you on the MSDN site because of IE settings

Tools - Internet Options - Security - Internet - Custom level

Scroll down to "Scripting", "Allow Programmatic clipboard access"
 
I don't actually know the answer, so what follows is just theory...

Firstly, the CopyCode function doesn't appear in the page's View Source, so
I'm guessing it's in some JavaScript include file...Therefore, it's
impossible to tell what it's doing without some serious hacking...

Secondly, this appears to be an IE-only thing - the "Copy Code" hyperlinks
don't appear at all when the page is viewed in any other browser, even the
very latest version of FireFox (2.0.0.4)...

Thirdly, I'm not actually certain that it's doing anything cleverer than
popping a showModalDialog and storing the return value in a JavaScript
variable... Simple enough to do but, of course, showModalDialog is not
cross-browser compatible and most certainly not standards-compliant...
Curiously enough, the "Copy Code" hyperlinks don't appear on Safari either,
even though showModalDialog *is* supported on that browser - draw your own
conclusions there... ;-)


In which case, you need to change your <script> tag to:

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

as the language tag is deprecated in XHTML...

Upper-case tag properties aren't supported either...

--http://www.markrae.net

thanks. onward, to the next challenge ...
 
What you can do is this:

function ShowClipBoard()
{
if ( confirm("Do you want to copy this to the clipboard?"))
{
var data = window.clipboardData.getData("text") ;
alert( data ) ;
}
}

Cheers!
Ray
 
I positive it's because localhost is the different zone (Local intranet)
than microsoft.com which's in Internet.
And different zones have different security settings

so as soon you move your code to domain.com you will be prompted as well

George.
 
Back
Top