简体   繁体   中英

Javascript : how to get value from a Script Tag

I am trying to get the following value from the script below Use this field to store the Account to which the Opportunity is related

<script>sfdcPage.setHelp('Opportunity.Account', 'Use this field to store the Account to which the Opportunity is related');</script>

i can get to this script tag by using :

document.getElementsByTagName('tbody')[6].getElementsByTagName('script')[1];

Can you please let me know. Many thanks.

I would not recommend putting script tags within your tables, etc. In other words, I wouldn't mix script tags with other content-related HTML like that. If you want to store little bits of info like that in your HTML for the purpose of JS, I'd use data attributes instead.

The simple answer to your question though is to just use the innerHTML or innerText property as follows:

document.getElementsByTagName('tbody')[6].getElementsByTagName('script')[1].innerHTML;

Use innerHtml as

document.getElementsByTagName('tbody')[6].
    getElementsByTagName('script')[1].
       innerHtml;

OR
Set an id to get script content as below example

<script id='script' type='text/javascript'>
    $('div').html($('#script').html());

    console.log($('script'));
</script>

fiddle

There are lots of ways to do this. One solution....

 var scrs = document.getElementsByTagName('script');
 var newscr = "";
 for (var i=0; i<scrs.length; i++) {
    newscr = scrs[i].innerHtml.replace('sfdcPage.setHelp(', 'myfunction(');
    if (newscr != scrs[i].innerHtml) {
       eval(newscr);
    }
 }

(although parsing / rewriting the javascript is an ugly hack - if it's your code then your reading then you should know what you put in there).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM