PDA

View Full Version : how to change the code below



jass
02-18-2007, 01:36 PM
In the index page, I have a link where if clicked, it will go to another webpage. Would appreciate if someone could advise on how to change the code below so that the URL of the new webpage remains the same as the index URL. Currently, when the link is clicked, it opens another window with the URL of the new webpage.

Link in the index html file:
<a href="javascript://" onClick="topic1()">

Code of the js file:
<!--
// topic1()
function topic1(){
<span onClick="topic1()">
window.open("../topic1.htm","topic1");
}
//-->

pcbrainbuster
02-18-2007, 09:25 PM
Hello DUDE,

If I am right what you want is to be able to open a link in the same window and not in a new one... Well if this is the case then all you have to do is -
replace

window.open

with

window.location

And that is all that it is to it !!!
window.open - opens new windows
window.location - opens links in the same tab/page

AND you are most WELCOME !!!:)

mburt
02-18-2007, 09:30 PM
function topic1(){
<span onClick="topic1()">
window.open("../topic1.htm","topic1");
}
^^ Is invalid for one. You can't nest HTML in JavaScript code.
By default, the window.open() function opens in a new window regardless. Although what pcbrainbuster said is correct, you should just do a link:

<a href="../topic1.htm">...
Some people may not be able to see JavaScript (their browser may think it to be a virus of some sort - in IE 5 happens all the time), and that's why you try to avoid it in places where you can.

pcbrainbuster
02-18-2007, 09:33 PM
Yeah what Mike said about the link is true in some cases is better just using javascript for anything YOU can, keeps practice...

(although i would personally use a link - lol)

Another thing is you can kind of nest html using the document.write method - if you need some help in this section just ask :) ( am at this forum about 8 hours a day(started today(lol)))

mburt
02-18-2007, 09:39 PM
document.write completely erases the page when used in the <head>. If you ever have to come to writing HTML with JavaScript, you should you the DOM method (appendChild(); ): http://slayeroffice.com/articles/innerHTML_alternatives/

jass
02-19-2007, 01:25 PM
Thanks to both for your kind response. The codes that I used were from a free script that I downloaded from the net for my site. However I hope to change it so that a new window will not be opened and because I do not wish the URL to be displayed, I did not use the <a href> method.

1) PCbrainbuster, I tried replacing window.open with window.location as appended below in the js file but it didn't work. Nothing happened. I wonder if I missed any thing?

Code of the js file:
<!--
// topic1()
function topic1(){
<span onClick="topic1()">
window.location("../topic1.htm","topic1");
}
//-->

2) Mburt, would appreciate if you could advise on which DOM method (appendChild(); method to use. There are quite a number listed there and I am at a loss.

pcbrainbuster
02-19-2007, 05:11 PM
For all I know what you want is a link to a topic.html with no info in the status bar (the bottom thing giving info of links...) and so i will make the script for you - if this is a success i will feel good and if not bad lol -
---------
IE
---------
<script>
function topic1() {
window.location="http://www.(full path here, it is easier that way)"
}
</script>

Just remove everything between including the brackets and type down your site address there between the the two quotes. Also give the button an event to fire the script. This script should just open the site in the window and that's it.

And now i will show you how to create a script that will display nothing in the status bar !!!

<script>
function removestatus() {
window.status=""
}
</script>

put your message between "" or leave it to keep the bar blank also set the event for the script in all the links - use onmouseover or onmouseenter.

To set the bar back to default replace window.status with window.status=window.defaultStatus
and use onmouseout or onmouseleave for that.

Does it help???:)

jass
02-20-2007, 12:20 AM
Thank you very much for your time and patience.
I tried both script that you gave but they didn't work:(

pcbrainbuster
02-20-2007, 07:23 AM
Hmmm, i do not know exactly what went wrong seeing how they work for me.

jass
02-20-2007, 08:12 AM
I wonder if I misinterpreted you earlier? Appended below are the codes that I used based on the one that you provided:

Link in the index html file:
<a href="javascript://" onClick="topic1()">

Code of the js file:
<script>
function topic1() {
window.location="http://www.domain/topic1.htm"
}
</script>

mburt
02-20-2007, 01:56 PM
You must return to the point at which you set the window.status to '':

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function direct() {
window.location.href = "http://www.google.ca/";
}
</script>
</head>
<body>
<a href="#" onmouseover="window.status=''; return true" onclick="direct()">link</a>
</body>
</html>

pcbrainbuster
02-20-2007, 05:02 PM
Please just type down your whole script/code on this page and i will do everything and disply it here for you, OK?:)

pcbrainbuster
02-22-2007, 04:35 PM
Ave ya tried,

<html>
<head>
<body>
<a onClick="topic1()">link</a><br><br>
<a onClick="topic2()">link</a><br>
<script>
function topic1() {
window.location="topic1.htm"
}
</script>
<script>
function topic2() {
window.location="topic2.htm"
}
</script>
</body>
</head>
</html>

pcbrainbuster
02-22-2007, 04:43 PM
This script is surely to work on IE andni do not no about the othhers....

mburt
02-23-2007, 12:07 AM
If you don't assign an "href" attribute for the <a> tag it will only be a page anchor, and it won't lead anywhere (also doesn't have the basic features of a link: text-underline, colors, etc.). Use href="#" if it becomes necessary.

mburt
02-23-2007, 12:09 AM
Also, remember to include scripts in the <head> when they have nothing to do with any elements:


<html>
<head>
<script>
function topic1() {
window.location="topic1.htm"
}
function topic2() {
window.location="topic2.htm"
}
</script>
</head>
<body>
<a href="#" onClick="topic1()">link</a><br><br>
<a href="#" onClick="topic2()">link</a><br>
</body>
</html>

jass
02-23-2007, 12:19 AM
Thank you so much to both pcbrainbuster and mburt.:p
I wonder if it is possible not to disclose the "topic1.htm" in the HTML code?

In my earlier sample, the location of "topic.htm" is revealed only in the js file.

mburt
02-23-2007, 12:51 AM
This code ONLY works for topic1.htm, if you need anything else encrypted just ask :)

var string = "116111112105099046104116109 "
var sep = string.match(/\d{3}/g)
var topic1 = ""
for (i=0;i<sep.length;i++) {
topic1 += "&#"+sep[i]
}
window.location.href = topic1

mburt
02-23-2007, 01:01 AM
After thinking it over... FireFox reads &# as it should, IE reads as text, which is what I was hoping for. You'll need this:

<html>
<head>
<script type="text/javascript">
function html(str) {
tohtml.innerHTML = str;
var tohtm = document.getElementById('tohtml').firstChild.nodeValue;
tohtml.innerHTML = "";
return tohtm;
}
function setURL(code) {
var string = code;
var sep = string.match(/\d{3}/g);
var topic = "";
for (i=0;i<sep.length;i++) {
topic += html("&#"+sep[i]);
}
return topic;
}
function topic1() {
window.location.href = setURL("116111112105099049046104116109");
}
function topic2() {
window.location.href = setURL("116111112105099050046104116109");
}
</script>
</head>
<body>
<div id="tohtml"></div>
<br><a href="javascript:topic1()">link</a><br><br>
<a href="javascript:topic2()" onClick="topic2()">link</a><br>
</body>
</html>

However, the url will still be displayed in your browser.

mburt
02-23-2007, 01:10 AM
If you need to parse any urls use this encoder:

<html>
<head>
<script type="text/javascript">
var test = "http://www.google.ca"
var str = ""
for (i=0;i<test.length;i++) {
if (test.charCodeAt(i) < 100) {
str+="0"+test.charCodeAt(i)
}
else {
str+=test.charCodeAt(i)
}
}
document.write(str)
</script>
</head>
<body>
</body>
</html>

jass
02-23-2007, 02:18 AM
If you need to parse any urls use this encoder:

<html>
<head>
<script type="text/javascript">
var test = "http://www.google.ca"
var str = ""
for (i=0;i<test.length;i++) {
if (test.charCodeAt(i) < 100) {
str+="0"+test.charCodeAt(i)
}
else {
str+=test.charCodeAt(i)
}
}
document.write(str)
</script>
</head>
<body>
</body>
</html>

Many thanks to mburt. Would appreciate if you could elaborate on how to use the above code basing on the earlier egs.

mburt
02-23-2007, 02:24 AM
The result for that variable will output a long number. You apply that number to the "string" variable on the previous page:

<html>
<head>
<script type="text/javascript">
function html(str) {
tohtml.innerHTML = str;
var tohtm = document.getElementById('tohtml').firstChild.nodeValue;
tohtml.innerHTML = "";
return tohtm;
}
function setURL(code) {
var string = code;
var sep = string.match(/\d{3}/g);
var topic = "";
for (i=0;i<sep.length;i++) {
topic += html("&#"+sep[i]);
}
return topic;
}
function topic1() {
window.location.href = setURL("116111112105099049046104116109");
}
function topic2() {
window.location.href = setURL("116111112105099050046104116109");
}
</script>
</head>
<body>
<div id="tohtml"></div>
<br><a href="javascript:topic1()">link</a><br><br>
<a href="javascript:topic2()" onClick="topic2()">link</a><br>
</body>
</html>

The above in red is what you replace with the supplied variable in the encoder. Just ask if you run into any problems.

jass
02-23-2007, 03:12 AM
Sorry, I do not quite understand what you meant by
You apply that number to the "string" variable on the previous page:

Thank you so much for your kind assistance.May I know how you obtain the number "116111112105099049046104116109"?