Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I doubt it's possible as I've tried, but is there a way to essentially have your code like this:

HTML
<script>
function(){
//do stuff
</script>
<script>
//do more stuff
}
</script>

This may seem like it would be useful to do, but maybe instead of another script tag i would add a js file that would be edited by PHP to add more stuff to the function....
Posted
Comments
Kornfeld Eliyahu Peter 15-Feb-15 15:00pm    
Why should you do such thing?
G4mm4R4y 16-Feb-15 1:55am    
The exact reason I stated above...
Sergey Alexandrovich Kryukov 16-Feb-15 2:00am    
Where? What reason?
—SA
G4mm4R4y 16-Feb-15 1:57am    
I would have it more like this:

<script>
function(){
//stuff
</script>
<script src="script.js"></script>
script.js would contain simply this:

//other stuff
}
Sergey Alexandrovich Kryukov 16-Feb-15 2:01am    
Do you think that repetition of the same thing makes it clearer? :-)
—SA

Not that it's possible or not, it would simply be useless or, rather, harmful.

JavaScript rules are much more reasonable than what you want (more exactly, what you probably think you want, as I cannot imagine that any one in sober mind would consciously want such a weird thing). You can create some object in one script and work with it in another, anyway, the boundary between object definition or statement (call, assignment, and the like). And a function definition is no different from any other object definition, as functions are first-class citizen.

Moreover, you should try to achieve just the opposite: even withing a single script, the code should be structured as much as possible, and the detail should be hidden from the parts of code where they are not used as well as possible. For example, of some object is used only inside some function, it should be made local, even if this is a function (inner functions are allowed and are important for better encapsulation. Objects should not be shared through the outer context but passed as function parameters and… so on.

—SA
 
Share this answer
 
Comments
Mohibur Rashid 17-Feb-15 3:30am    
I always respect you as an expert. But this time I will not agree with you. We are always using this types of thing. If we do not know, then it is our fault. when we call $(obj).html() we are doing the whole process. JQuery is handling this.
Sergey Alexandrovich Kryukov 17-Feb-15 11:16am    
Thank you, Mohibur.
Perhaps I did not understand what the inquirer mean (but I think this is explained), but I certainly don't understand what do you mean. Would you explain? What "this"?
—SA
Mohibur Rashid 17-Feb-15 21:58pm    
Withdrawing my comment.
Sergey Alexandrovich Kryukov 17-Feb-15 22:09pm    
Thank you, Mohibur.
I only want to say how much I appreciate your critical thinking, correct logical discussion, ability to adequately deal with criticism, and other important components of objective and scientific approach.
—SA
Mohibur Rashid 18-Feb-15 1:25am    
I want to add your final comment in my resume :D
You can not split a function in the half (I do not know any language that allow such a thing)...
If you need some dynamic functionality that somehow extend the existing you have to create an other solution...
There is a nice JavaScript library to do so: http://extendjs.org/[^]
 
Share this answer
 
Writing JavaScript like this is a bad idea. Apart from the good solutions provided here I would say to learn about unobstrusive javascript implementation instead of mixing up everything together in your web page.
 
Share this answer
 
Upon further reading, according to SA's suggestion, I understood your question.
I would try to give you another answer:
The thing you are trying to achieve is not possible. Similar thing is very much possible in PHP though. But if you have section that need to be showed based on various function and conditions then I would suggest you to look through the option of hiding and showing or creating and destroying the object based on function call.



--text, written below, seems unrelated--

I do not think you have explained your question good enough to have an answer.
I will explain the reason, that I have understood.

Say, your current browser received a data from server like below:
JavaScript
<script ...="">
function process_new_div(){
...
}
process_new_div();
</script>
<div id="new_div">
 content.
</div>


Current page will load this portion of data and then process the received code. This issue will only occur with AJAX.

Solution:
In general, most of the developer use JQuery to solve this issue. JQuery will dissect the received data and process the Script for you. If you are not using jquery
here is an example below
JavaScript
var result = received_data.match(/<script>(.*?)<\/script>/g).map(function(val){
   return val.replace(/<\/?script>/g,'');
});
</script>


In order to run extracted javascript:
run eval function. Try the following example in firefox or Chrome browser:
JavaScript
eval("x=10;alert(x)");


Anyway, if this is not what you want let us know.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Feb-15 11:24am    
Perhaps you are the one who did not understand the inquirer's "idea". But it looks so weird that...
Look again on that code "sample". I'll translate it a bit:

function someFunction() {
// do something

// the part above is written in one script element
// the part below is written in another script element

// do something else
}


Doesn't it really like absurd? And you are showing something very different in your samples, something much more reasonable and not absurd. All right, but why?

If you wanted to put some different idea in the inquirer's mouth, you would need to explain it this way:
"It looks like you wanted to suggest A, but I think that you really need B and just did not explain it properly. If so, here is my advice..." Without it, your post looks like something on unrelated topic.

And, if you realize what the inquirer's formulation is, probably you will finally understand my answer.

—SA
Mohibur Rashid 17-Feb-15 21:15pm    
If I were offensive with my comment, then I am sorry, I didn't mean it. But, I am not trying to put anything on behalf of the op. My answer is based on the fact that would cause the situation where it is required to separate script from the HTML code. In general I don't see any reason to or option to separate script and html when the page is loaded. This situation might occur when your data is in string format. Another possibility, to have the code anywhere else other than the header, is if developer put his code inside html body, which not rare. I have seen such case a lot. If this is case, part of my answer is still valid. And also, even though the body does not say anything about splitting tag but the title of the question says splitting tag.

And again, I don't want to offense you.
Sergey Alexandrovich Kryukov 17-Feb-15 21:44pm    
First of all, no way I would consider your posts offensive. You conduct your opinions in a very correct and considerate way.

I just think you misread the inquirer's message, in my interpretation of in original. You are talking about separation of script and HTML. But look thoroughly: don't you see that the inquirer, by some weird reason, is asking about breaking the same JavaScript function in the middle between two different script element. Completely different situation. Please look again. Can you see it?

—SA
Mohibur Rashid 17-Feb-15 21:50pm    
Thank you for quick reply. I read it again and I apologizes for my miss reading. I actually emphasize the title which says 'Split Javascript Code Between Tags' and I understood as "retrieving javascript code from tag". Now I have understood the absurdity of the question or requirement. Thank you for being patient. I will update my answer.
Sergey Alexandrovich Kryukov 17-Feb-15 21:57pm    
Also, you may want to review my answer, in view of new understanding. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900