Depending on which page I am, I would like the content of a javascript function to change.
Example :
MasterPage
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>
Page A
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>
Because I do not want to do code repetition, I’m looking to a solution. I thought that maybe I could use something like a ContentPlaceHolder like this (but it doesn’t work) :
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
<asp:ContentPlaceHolder id="cphJS" runat="server" />
</script>
Any way to solve this kind of problem ?
UPDATE
What I will like to avoid is to have code like that :
// Add by MasterPage
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
</script>
// Add by Page A
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
</script>
// Add by Page B
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>
Because that kind of code work but it’s not the cleaning way of doing it. I would prefer to have something like that :
<script type="text/javascript">
Cufon.now();
Cufon.replace('p#characters', { fontFamily: 'Helvetica95-Black' });
Cufon.replace('p#others', { fontFamily: 'Helvetica95-Black' });
Cufon.replace('p#main', { fontFamily: 'Helvetica95-Black' });
Cufon.replace('p#menu', { fontFamily: 'Helvetica95-Black' });
</script>
The result is the same. I only find that the second output is cleaner.
Why not simply add the additional code in a content place holder outside of the script tag?
So, you would have something like:
Or, you might collect functions in an array early in the page and then execute them later on like so:
In the <head>:
At some other location:
And then at the bottom of the page, just before the </body>:
Could you provide more details about the requirements? Ex: What the above code is supposed to do, does it have to be executed all at once, does it depend on local variables that are created, does it have to run at a certain time, etc.
Edit:
Using a content placeholder in a new MVC project seemed to work fine for me. Intellisense didn’t pick it up but it rendered correctly. Here’s what the code looks like for me.
In the master page:
…
In the page:
And the rendered HTML: