How to make a button's onclick function in one webpage execute a function in another webpage without php

时间:2016-10-20 13:02:29

标签: javascript html

In my main html page, I have the following code:

<p class="text" id="time">00:00</p>

In my other html page, I have the following code:

<button onclick="resettime()">RESET TIME</button>

They both link to the same javascript page, which has the code:

function timeIncrement()
{
    document.getElementById('time').innerHTML = +mins+":"+secs;
  if(secs==60)
  {
    mins+=1;
    secs = 0;
  }
}

var timecheck = setInterval(timeIncrement,1000);

function resettime()
{
  clearInterval(timecheck);
  mins  = 0;
  secs = 0;
  timecheck = setInterval(timeIncrement,1000);
}

Clicking the button doesn't reset the time to 0:0, so it doesn't work, so I was wondering if there's any way I can do this without going into php. I'm new to html/js, so sorry if this is a repeat, couldn't find a similar one.

1 个答案:

答案 0 :(得分:0)

两个html意味着两个窗口。如果你想从一个窗口执行脚本到另一个窗口,你应该有另一个窗口的对象,并且两个窗口都应该有document.domain。用于引用另一个窗口

在主html中添加以下内容

NSDate *date = [NSDate dateWithTimeIntervalSince1970:1483620311.228]; 
NSLog(@"current date ===> : %@", date);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *previousMonday = [calendar nextDateAfterDate:date 
                                        matchingUnit:NSCalendarUnitWeekday  
                                               value:2 
                                             options:NSCalendarMatchNextTime | NSCalendarSearchBackwards];
NSLog(@"previousMonday date ===> : %@", previousMonday);

在其他html中,将代码更改为以下

<script>window.open("other.html");//other html will open automatically</script>