/    Sign up×
Community /Pin to ProfileBookmark

javascript problem

I got a problem with javascript, that i want to print out hyperlink’s contents of the webpage, which without open the webpage. Now, i had writen some codes, which intend to do this task.
But i had meet a problem….so can anyone give me a hand?

<html>
<head>
<title></title>
<script>
function printURL(sHref) {
if(document.getElementById && document.all && sHref){
if(!self.oPrintElm){
var aHeads = document.getElementsByTagName(‘HEAD’);
if(!aHeads || !aHeads.length)
return false;
if(!self.oPrintElm)
self.oPrintElm = document.createElement(‘LINK’);
self.oPrintElm.rel = ‘alternate’;
self.oPrintElm.media = ‘print’;
aHeads[0].appendChild(self.oPrintElm);
}
self.oPrintElm.href = sHref;
self.focus();
self.print();
return true;
}
else return false;
}

function linklist(){
for (var i = 0; i < document.links.length; i++)
document.write(document.links[i]);
}
</script>
</head>
<body>
<a target=”_blank” href=”javascript:linklist()” onclick=”printURL(this.href); return false;”

>print</a><br><br>
<br>
<a href=”http://www.google.com”>Google.com</a>
<br>
<a href=”http://www.yahoo.com”>Yahoo.com</a>
</body>
</html>

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 10.2005 — Cross-domain scripts don't work for security reasons, sorry.
Copy linkTweet thisAlerts:
@derek123authorMay 10.2005 — Cross-domain scripts don't work for security reasons, sorry.[/QUOTE]
so.....any other solutions?
Copy linkTweet thisAlerts:
@UltimaterMay 10.2005 — Bookmarks ? hehe

I'm not sure, but, maybe a name like myName.yahoo.com would be able to access the search.yahoo.com page. Maybe you can get a webpage there....

I don't think Yahoo gives sub-domains though... ?

I'm sure there's other search engines that provide such a service ?
Copy linkTweet thisAlerts:
@derek123authorMay 10.2005 — Bookmarks ? hehe

I'm not sure, but, maybe a name like myName.yahoo.com would be able to access the search.yahoo.com page. Maybe you can get a webpage there....

I don't think Yahoo gives sub-domains though... ?

I'm sure there's other search engines that provide such a service ?[/QUOTE]

ummm....my problem is that script not working..(bug)...i think that not related to "cross-domain" right? :p since "www.yahoo.com" just a example, i am not really put it on. ?
Copy linkTweet thisAlerts:
@UltimaterMay 10.2005 — Do you mean that your webpage has nothing to do with yahoo except that you have a hyperlink to it?

Do you mean that when a user prints your webpage that you want to change the [i]style[/i] of the page?

If so then you should post this in the CSS forum.
<i>
</i>&lt;style type="text/css" media="print"&gt;
/*CSS code goes here*/
&lt;/style&gt;

The above is what your code will probably need to look like.

media="print" means that the CSS nested inside of the the current STYLE tag will only aply-to/execute-on the page's look that the printer will "see".
Copy linkTweet thisAlerts:
@derek123authorMay 10.2005 — Do you mean that your webpage has nothing to do with yahoo except that you have a hyperlink to it?
[/QUOTE]

yes....thats right.

the problem i am facing now is.."href="javascript:linklist()" can't received the value from the "function linklist()", and i had no idea why it doesn't work.. ?
Copy linkTweet thisAlerts:
@UltimaterMay 10.2005 — Screw the function, all you gotta do is:

[color=royalblue]<a href="javascript:[color=royalblue]print()">print</a>[/color][/color]

Then you'll need to provide a STYLE tag somewhere in your HTML with the media="print".

For example, if you didn't want a layer to show-up when it's printed but be visible on the computer screen, you'd do:
[code=html]
<style type="text/css" media="print">
.myLayer{
display: none;
}
</style>
[/code]
Copy linkTweet thisAlerts:
@derek123authorMay 10.2005 — Screw the function, all you gotta do is:

[color=royalblue]<a href="javascript:[color=royalblue]print()">print</a>[/color][/color]

Then you'll need to provide a STYLE tag somewhere in your HTML with the media="print".

For example, if you didn't want a layer to show-up when it's printed but be visible on the computer screen, you'd do:
[code=html]
<style type="text/css" media="print">
.myLayer{
display: none;
}
</style>
[/code]
[/QUOTE]

i am intend to use "<a href="javascript:linklist();">print</a>" to retrieve the value from "function linklist()", the use "printURL(sHref)" function to print out the result.
Copy linkTweet thisAlerts:
@UltimaterMay 10.2005 — Try merging the following together:
<i>
</i>href="javascript:linklist()" onclick="printURL(this.href); return false;"

into:
<i>
</i>href="javascript:void(linklist());void(printURL(this.href))"


Also, I don't think that [color=royalblue]target[/color] works with [color=royalblue]href="javascript:..."[/color]

You should omit the target part and create the new window via the function.
Copy linkTweet thisAlerts:
@derek123authorMay 11.2005 — i had change the code as follow, but seem the "document.links" can not pass all the values (there suppose 2 links) to the function call. Does anyone has any idea on it?

thanks~


<html>

<head>

<SCRIPT LANGUAGE="JavaScript">

function DoPrintLinks()

{

var nCount = 0;

var nEnd = document.links.length;

<!--document.write("According to JavaScript, this document has ",nEnd, " link(s) in it.")-->

for (nCount = 0 ; nCount < nEnd ; nCount ++){

document.write(document.links[nCount].href + "<br>");

}

}

function printURL(sHref) {

if(document.getElementById && document.all && sHref){

if(!self.oPrintElm){

var aHeads = document.getElementsByTagName('HEAD');

if(!aHeads || !aHeads.length)

return false;

if(!self.oPrintElm)

self.oPrintElm = document.createElement('LINK');

self.oPrintElm.rel = 'alternate';

self.oPrintElm.media = 'print';

aHeads[0].appendChild(self.oPrintElm);

}

self.oPrintElm.href = sHref;

self.focus();

self.print();

return true;

}

else return false;

}


</script>

</head>

<body>

<input type="button" value="test" onclick="javascript:printURL(DoPrintLinks());"><br><br>

<input type="button" value="test" onclick="DoPrintLinks();"><br><br>

<a href = "http://www.google.com">google</a><br>

<a href = "http://www.yahoo.com">Yahoo</a>

</body>

</html>
Copy linkTweet thisAlerts:
@UltimaterMay 11.2005 — Use this function instead:

<i>
</i>function DoPrintLinks()
{

var nCount = 0;
var nEnd = document.links.length;
var outpt="According to JavaScript, this document has "+nEnd+ " link(s) in it.&lt;br&gt;"
for (nCount = 0 ; nCount &lt; nEnd ; nCount ++){
outpt+=document.links[nCount].href + "&lt;br&gt;"
}
document.write(outpt)
}
Copy linkTweet thisAlerts:
@derek123authorMay 11.2005 — Use this function instead:

<i>
</i>function DoPrintLinks()
{

var nCount = 0;
var nEnd = document.links.length;
var outpt="According to JavaScript, this document has "+nEnd+ " link(s) in it.&lt;br&gt;"
for (nCount = 0 ; nCount &lt; nEnd ; nCount ++){
outpt+=document.links[nCount].href + "&lt;br&gt;"
}
document.write(outpt)
}
[/QUOTE]


Actually, my final purpose is to print out all the hyperlink's contents, without

open each link. Seems i had a big problem with the codes. :o
Copy linkTweet thisAlerts:
@BigMoosieMay 11.2005 — ...maybe a name like myName.yahoo.com would be able to access the search.yahoo.com page...[/QUOTE]
I reaslise that this isnt very relavent to the discussion here but I am just letting you know that this will not work. An example of what would [B]not[/B] be accessible by [B]http://subdomain.site.com/[/B]:

[list]
  • [*]http://subdomain[COLOR=Red][B]2[/B][/COLOR].site.com/

  • [*]http://subdomain.site[COLOR=Red][B]2[/B][/COLOR].com/

  • [*]http[COLOR=Red][B]s[/B][/COLOR]://subdomain.site.com/

  • [/list]
    ×

    Success!

    Help @derek123 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.5,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...