homepage Welcome to WebmasterWorld Guest from 54.198.140.148
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
How to Call A Specific Line from a Javascript File?
1 line out of 100 per post
adder




msg:4375406
 9:56 am on Oct 17, 2011 (gmt 0)

Hi,

This is a bit challenging and I cannot find an answer in my JS book.

Let's say I have a file myscript.js that consists of 100 lines, each line corresponding to a particular action:

1. document.write('<p>something</p>');
2. document.write('<p>something else</p>');
3. document.write('<p>something entirely different</p>');
etc...


I'm writing a wordpress post and think: ok, I need to embed line 3 in this post.

Can I use some sort of a placeholder to call the line 3? Something like this:


<script language="javascript" src="myscript.js#3">
</script>


Of course, I understand that this example wouldn't work, I'm just trying to illustrate what I'm trying to achieve.

Or maybe I can assign each action to a variable and then call that unique variable with my embed file?

 

penders




msg:4375414
 10:46 am on Oct 17, 2011 (gmt 0)

You'll need to use PHP to extract the relevant line from the file. You'll then need to wrap this in <script> tags before writing it out to the page. You won't find a JavaScript only solution to this.

rocknbil




msg:4376055
 5:56 pm on Oct 18, 2011 (gmt 0)

Something like this? Remember that arrays start at zero, the first member is phrases[0].


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
var phrases = [
'<p>something</p>',
'<p>something else</p>',
'<p>something entirely different</p>'
];
</script>
</head>
<body>
<p> bla blah blah<strong>
<script type="text/javascript">document.write(phrases[1]);</script>
</strong>more blah blah blah.
</body>
</html>


if you need to execute them, one of the extremely rare cases where eval would help:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
var phrases = [
"document.write('<p>something</p>')",
"document.write('<p>something else</p>')",
"document.write('<p>something entirely different</p>')"
];
</script>
</head>
<body>
<p> bla blah blah<strong>
<script type="text/javascript">eval(phrases[1]);</script>
</strong>more blah blah blah.
</body>
</html>


Kind of a workaround, but not specifically line by line. I suppose you could write up a PHP/Perl/ASP script that slurps up your .js and outputs the entire thing as an array like the example, then call that like

my-php.php?arraynum=1

adder




msg:4376438
 8:40 am on Oct 19, 2011 (gmt 0)

Oh, that's interesting. Array might help.

The thing is that in wordpress posts (for some strange reason) I can only add javascript this way:

<script language="javascript" src="myscript.js">
</script>

If I add some operations, they just get stripped when I hit "Post". To solve this I would have to edit the core which I'm reluctant to do because I'd have to do it again each time WP issues an update.

I think you're both right - I'll end up doing something in PHP. I'd probably need a script that creates and writes those javascript files automatically so that I can simply insert a unique .JS file with each post:

<script language="javascript" src="wp-content/myjs/myscript-id-12345.js">
</script>

Skier88




msg:4376713
 7:01 pm on Oct 19, 2011 (gmt 0)

If you can't use an array then php is probably the best solution, but, in case you were still wondering, a javascript solution is simple as well. Example: include "<script type='text/javascript' src='thisscript.js?line=3'></script>" in any file to execute only the specified line of myscript.js.

var line=/\?(?:.*&)*line=(\d)+/.exec(document.location),
req=window.XMLHttpRequest?
new XMLHttpRequest():
new ActiveXObject('Microsoft.XMLHTTP');
line=+line[1];
req.onreadystatechange=function() {
if(this.readyState==4) {
this.status==200?
eval(this.responseText.split(/\r?\n/)[line]):
alert('error');
}
};
req.open('GET','myscript.js',true);
req.send();

Disadvantages: client makes an extra request (=slower), can't load myscript.js from another domain. Advantages: reduces server load.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved