A subtle but potentially critical struggle for the attention of President Reagan is going on within the confines of the White House.

It is a battle between those who want the Reagan focus to remain squarley on the economic program he has presented to the country and those who are eager for an early test of U.S. assertiveness against communist regimes.

For the first time in Reagan's presidency, the advocates of domestic focus are on the defensive. Encouraged by Secretary of State Alexander M. Haig Jr. and national security adviser Richard V. Allen, Reagan's old preoccupation with anticommunism came tothe fore this week.

Only a week ago the president himself was reminding aides that solving what he called "the worst economic mess since the Depression" was the primary issue before him.

"If we do well on the economy, the honeymoon expands; if we don't, it contracts," was the way a top aide explained it last week.

For this reason, the White House focus in February was supposed to be solely on the economy. Buttressing the case for domestic emphasis, one aide even quoted from advice given Reagan by former president Richard Nixon before the inauguration. The advice was to spend the first six months of the administration concentrating solely on getting the economic house in order.

Reagan seemed to be securely on this track until 10 days ago, when he received a national security briefing that painted a dire picture of the consequence of continued Cuban arms supplies to Salvadoran rebels. From that moment, as one source put it, "you begin to hear more about Haig and less about [David A.] Stockman," the director of the Office of Management and Budget.

By implication, this also was at least a temporary shift in attention away from Stockman's consistent supporters -- counselor Edwin Meese III and chief of staff James A. Baker III -- who have a domestic orientation rather than a foreign one, although Reagan has been known to consult Meese on nearly every issue.

This week in the White House, Reagan's attention, according to some aides, seemed to have shifted to foreign policy. He did not even learn of the error in budget estimates that sent Stockman back to the drawing boards until at least 48 hours after it had been discovered.

The presidents's foreign policy focuswas readily apparent Monday, when he awarded a Medel of Honor at a Pentagon ceremony and took the occasion to declare his long-held view the U.S. military forces were defeated in Vietnam because they "were denied permission to win."

Aides say that Reagan was pleased by the loud applause from the military men and their families massed in the Pentagon courtyard. This week the phrase, "new Monroe doctrine," was used in White House circles to describe the policy that Haig is enunciating toward "foreign intervention" in El Salvador -- even though the old Monroe Doctrine referred to European intervention in this hemisphere, and the primary culprit now is Cuba.

But there are those in the White House who believe that Reagan will stay on the economic track instead of becoming preoccupied with El Salvador.

"We're working very hard not to let that happen; it isn't in our best interests collectively," said one Reagan aide yesterday.

While it often is said that Reagan's view of foreign policy is rooted in World War II, his view of the Vietnam war actually derives from the Korean conflict.

Reagan was making his personal political odyssey from Democrat to Republican and liberal to conservative when the Korean war started. He had supported Harry Truman in 1948 (Dwight Eisenhower in 1952 became Reagan's first vote for a Republican presidential candidate) and he thought highly of the Truman presidency.

But Reagan was troubled by the restraints imposed on U.S. forces in the Korean War, restraints that led to Gen. Douglas MacArthur's removal when he refused to abide by them.

In an interview years later, Reagan said that Truman would have gone down in history as a great president, "except that he fought the first no-win war."

That view was recalled this week by alongtime Reagan supporter, who expressed the hope that the administration would not be "diverted" by El Salvador.

There are those in the White House who think that the only chance for Reagan's economic program is a "full-court press" in which the president himself plays the leading role. These aides say there is such a consensus in the country for budget cuts that even the Democratic House will find it irresistible if Reagan demonstrates leadership on the issue.

"There is unlikely to be such a consensus on El Salvador if we get beyond a war of words," said one White House aide this week.

But Reagan's own deeply held anticommunism, coupled with his conviction that the Korean and Vietnam conflicts were lost at home rather than on the battlefield, appears to be moving him rapidly in the direction some of his domestic advisers fear most.